您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

使用lxml在python中编码-复杂的解决方案

使用lxml在python中编码-复杂的解决方案

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的另一个库进行交互?

python 2022/1/1 18:31:46 有518人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶