【问题标题】:Preventing etree from resolving HTML entities when parsing HTML contents解析 HTML 内容时阻止 etree 解析 HTML 实体
【发布时间】:2014-04-11 09:32:43
【问题描述】:

在解析 HTML 内容时,有什么方法可以阻止 etree 解析 HTML 实体?

html = etree.HTML('<html><body>&amp;</body></html>')
html.find('.//body').text

这给了我 '&' 但我想得到 '&'自己。

【问题讨论】:

标签: python lxml elementtree


【解决方案1】:

您始终可以预先/后处理您的数据。 在提供给 HTML 解析器之前将 '&' 替换为 u'\xfe' 并在输出时将 u'\xfe' 替换为 '&'。

from lxml import etree
html = etree.HTML('<html><body>&amp;</body></html>'.replace('&',u'\xfe'))
html.find('.//body').text.replace(u'\xfe','&')
u'&amp;'

【讨论】:

    猜你喜欢
    • 2014-02-05
    • 1970-01-01
    • 2021-12-30
    • 2017-10-29
    • 1970-01-01
    • 1970-01-01
    • 2010-09-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多