[英]How to find recursively for a tag of XML using LXML?
<?xml version="1.0" ?>
<data>
<test >
<f1 />
</test >
<test2 >
<test3>
<f1 />
</test3>
</test2>
<f1 />
</data>
Using lxml is it possible to find recursively for tag " f1 "? 使用lxml是否可以递归查找标签“f1”? I tried findall method but it works only for immediate children. 我试过findall方法,但它只适用于直接的孩子。
I think I should go for BeautifulSoup for this !!! 我想我应该去为BeautifulSoup这个!!!
You can use XPath to search recursively: 您可以使用XPath递归搜索:
>>> from lxml import etree
>>> q = etree.fromstring('<xml><hello>a</hello><x><hello>b</hello></x></xml>')
>>> q.findall('hello') # Tag name, first level only.
[<Element hello at 414a7c8>]
>>> q.findall('.//hello') # XPath, recursive.
[<Element hello at 414a7c8>, <Element hello at 414a818>]
iterfind()
iterates over all Elements that match the path expression iterfind()
遍历与路径表达式匹配的所有元素
findall()
returns a list of matching Elements findall()
返回匹配元素的列表
find()
efficiently returns only the first match find()
有效地只返回第一个匹配
findtext()
returns the .text content of the first match findtext()
返回第一个匹配项的.text内容
Illustrative Examples: 说明性示例:
>>> root = etree.XML("<root><a x='123'>aText<b/><c/><b/></a></root>")
#Find a child of an Element:
>>> print(root.find("b"))
None
>>> print(root.find("a").tag)
a
#Find an Element anywhere in the tree:
>>> print(root.find(".//b").tag)
b
>>> [ b.tag for b in root.iterfind(".//b") ]
['b', 'b']
#Find Elements with a certain attribute:
>>> print(root.findall(".//a[@x]")[0].tag)
a
>>> print(root.findall(".//a[@y]"))
[]
Reference: http://lxml.de/tutorial.html#elementpath 参考: http : //lxml.de/tutorial.html#elementpath
(This answer is relevant selective selection from the content at this link) (这个答案是从这个链接的内容中选择相关的选择)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.