lxml对于输入编码可能有点不了解。最好发送UTF8并取出UTF8。
您可能要使用chardet模块或UnicodeDammit解码实际数据。
您想做些模糊的事情:
import chardet
from lxml import html
content = urllib2.urlopen(url).read()
encoding = chardet.detect(content)['encoding']
if encoding != 'utf-8':
content = content.decode(encoding, 'replace').encode('utf-8')
doc = html.fromstring(content, base_url=url)
我不确定为什么要在lxml和etree之间移动,除非与正在使用etree的另一个库进行交互?