请帮我解决lxml的问题
(我是lxml的新手).
如何从下一个文件中获取"评论1":
<?xml version="1.0" encoding="windows-1251" standalone="yes" ?>
<!--Comment 1-->
<a>
<!--Comment 2-->
</a>
Run Code Online (Sandbox Code Playgroud)
Joh*_*hin 12
文档:lxml教程,搜索"评论"
码:
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)
Run Code Online (Sandbox Code Playgroud)
输出:
=== 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'
Run Code Online (Sandbox Code Playgroud)
注释:不适用于xml.etree.cElementTree
>>> from lxml import etree
>>> tree = etree.parse('filename.xml')
>>> root = tree.getroot()
>>> print root.getprevious()
<!--Comment 1-->
Run Code Online (Sandbox Code Playgroud)
或者可以肯定(可能不止一个):
>>> for i in root.itersiblings(tag=etree.Comment, preceding=True):
... print i
...
<!--Comment 1-->
Run Code Online (Sandbox Code Playgroud)
.text如果要提取注释文本,请使用该属性.
| 归档时间: |
|
| 查看次数: |
6440 次 |
| 最近记录: |