【发布时间】:2012-02-29 01:57:51
【问题描述】:
我正在尝试在元素树中查找一些节点,但这似乎会根据我用于解析的实现而有所不同。这似乎与文档不一致。我错过了什么吗?
In [52]: ElementTree.fromstring('<html><x /></html>').find('.//x')
Out[52]: <Element 'x' at 0x3008c10>
但是:
In [59]: type(html5lib.parse('<html><x /></html>', treebuilder='lxml').find('.//x'))
Out[59]: <type 'NoneType'>
我也尝试过使用 ElementTree 的 html5lib,但这似乎并没有运行符合文档的解析:
In [72]: parser = html5lib.HTMLParser(tree=html5lib.treebuilders.getTreeBuilder('etree', cElementTree))
In [73]: type(parser.parse('<html><x /></html>'))
Out[73]: <type 'NoneType'>
那么我该如何解决呢?我无法继续直接使用 ElementTree,因为它不会解析一些损坏的 html。
【问题讨论】:
标签: python html parsing elementtree