请帮我解决lxml的问题
(我是lxml的新手).
如何从下一个文件中获取“评论1”:
(我是lxml的新手).
如何从下一个文件中获取“评论1”:
<?xml version="1.0" encoding="windows-1251" standalone="yes" ?>
<!--Comment 1-->
<a>
<!--Comment 2-->
</a>
文档:the lxml tutorial,并搜索“评论”
码:
import lxml.etree as et
text = """\
<?xml version="1.0" encoding="windows-1251" standalone="yes" ?>
<!--Comment 1a-->
<!--Comment 1b-->
<a> waffle
<!--Comment 2-->
blah blah
</a>
<!--Comment 3a-->
<!--Comment 3b-->
"""
print "\n=== %s ===" % et.__name__
root = et.fromstring(text)
for pre in (True, False):
for comment in root.itersiblings(tag=et.Comment, preceding=pre):
print pre, comment
for elem in root.iter():
print
print isinstance(elem.tag, basestring), elem.__class__.__name__, repr(elem.tag), repr(elem.text), repr(elem.tail)
输出:
=== lxml.etree ===
True <!--Comment 1b-->
True <!--Comment 1a-->
False <!--Comment 3a-->
False <!--Comment 3b-->
True _Element 'a' ' waffle\n ' None
False _Comment <built-in function Comment> 'Comment 2' '\n blah blah\n'
注释:不适用于xml.etree.cElementTree
相关文章
转载注明原文:使用lxml和Python进行XML解析 - 代码日志