Python 子线程里创建 BeautifulSoup 对象会在终端打印encoding error : input conversion failed due to input error, 主线程里面一切正常。
感觉应该是 lxml 的问题,但是没找到怎么解决,求解惑
就几个特定的网页会出现这个问题,比如 http://zhuanlan.sina.com.cn/
下面这个代码可以用来复现
import requests
from bs4 import BeautifulSoup
from threading import Thread
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
)