如何使用 LXML 递归查找 XML 标签?
<?xml version="1.0" ?>
<data>
<test >
<f1 />
</test >
<test2 >
<test3>
<f1 />
</test3>
</test2>
<f1 />
</data>
使用 lxml 是否可以递归查找标签“ f1 ”?我尝试了 findall 方法,但它仅适用于直系孩子。
我想我应该为此选择 BeautifulSoup !
<?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 "? I tried findall method but it works only for immediate children.
I think I should go for BeautifulSoup for this !!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 XPath 递归搜索:
You can use XPath to search recursively:
iterfind()
迭代与路径表达式匹配的所有元素findall()
返回匹配元素的列表find()
有效地仅返回第一个matchfindtext()
返回第一个匹配的 .text 内容说明性示例:
参考:
http://lxml.de/tutorial.html#elementpath
(此答案是从此链接的内容中进行的相关选择性选择)
iterfind()
iterates over all Elements that match the path expressionfindall()
returns a list of matching Elementsfind()
efficiently returns only the first matchfindtext()
returns the .text content of the first matchIllustrative Examples:
Reference:
http://lxml.de/tutorial.html#elementpath
(This answer is relevant selective selection from the content at this link)