如题,使用Python requests和BeautifulSoup,编写一个简单的函数test从网址生成soup对象(见下面的例子)。如果在主线程里面直接调用那么一切正常,但是如果在子线程里面调用这个函数就会出现encoding error : input conversion failed due to input error, bytes 0x95 0x50 0x22 0x20
这样的日志信息, 而且是直接输出到stdout的,没有抛出异常也就没法捕获。
请问有什么方法可以解决吗?
下面是一个可以复现的例子。
from threading import Thread
import requests
from bs4 import BeautifulSoup
def test():
r = requests.get('http://zhuanlan.sina.com.cn/')
soup = BeautifulSoup(r.content,'lxml')
print('在主线程中执行test')
test()
print('在子线程中执行test')
t = Thread(target=test)
t.start()
t.join()
输出内容如下
在主线程中执行test
在子线程中执行test
encoding error : input conversion failed due to input error, bytes 0x95 0x50 0x22 0x20
encoding error : input conversion failed due to input error, bytes 0x95 0x50 0x22 0x20
encoding error : input conversion failed due to input error, bytes 0x95 0x50 0x22 0x20