python - lxml 没有为 HTML 正确解析 unicode

标签 python unicode lxml

我正在尝试解析 HTML,但不幸的是 lxml 不允许我获取实际文本:

node = lxml.html.fromstring(r.content)
self.fingerprint['Title'] = node.cssselect('.document-title div')[0].text
print '@@####', self.fingerprint['Title']

# @@#### Démineurs

我需要做什么才能正确解析此文本?这是网页:https://play.google.com/store/movies/details/D%C3%A9mineurs?id=KChu8wf5eVo&hl=fr文本应为 Démineurs。

最佳答案

文档没有编码信息,因此您需要创建一个默认使用正确编码的解析器。

>>> lxml.html.fromstring('<p>é</p>').text
u'\xc3\xa9'
>>> hp = lxml.etree.HTMLParser(encoding='utf-8')
>>> lxml.html.fromstring('<p>é</p>', parser=hp).text
u'\xe9'

关于python - lxml 没有为 HTML 正确解析 unicode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29057188/

相关文章:

ios - 在 Objective-C 中将字符串转换为 unicode

Python:使用 lxml 从 Open Office calc 导入数据

python - 在Python中从SPSS访问标签时如何处理特殊字符?

python - Django 初始化所有测试类的数据测试

python - 为 Django URL Dispatcher 编写的测试不起作用

python - 即使方法中没有 'self' 属性,代码也能正常工作

javascript - 操作 JavaScript Unicode

python - 如何在 Django 中按 "application instance"使用不同的数据库?

javascript - html2canvas 无法正确渲染带有修饰符的表情符号

Python:无法导入lxml.etree.xml文件