更新时间:2021-12-07 22:14:40
comment()
是ElementTree不支持的XPath节点测试。
comment()
is an XPath node test that is not supported by ElementTree.
您可以使用注释()
与 lxml 。此库与ElementTree非常相似,完全支持XPath 1.0。
You can use comment()
with lxml. This library is quite similar to ElementTree and it has full support for XPath 1.0.
这是哟你可以使用lxml来删除注释:
Here is how you can remove comments with lxml:
from lxml import etree
XML = """<root>
<!-- COMMENT 1 -->
<x>TEXT 1</x>
<y>TEXT 2 <!-- COMMENT 2 --></y>
</root>"""
tree = etree.fromstring(XML)
comments = tree.xpath('//comment()')
for c in comments:
p = c.getparent()
p.remove(c)
print etree.tostring(tree)
输出:
<root>
<x>TEXT 1</x>
<y>TEXT 2 </y>
</root>