繁体   English   中英

当在python中使用lxml多次使用同一标签时,如何从xml标签中获取内容?

[英]How do I get content from xml tags when the same tag is used multiple times using lxml in python?

假设我有以下内容-

<test> This is some text </test>
<test> This is other text </test>

现在,我想从两个标签中提取内容,但是能够将它们分别称为tag-1和tag-2,以便我可以分别使用它们。 我怎么做?

使用root.findall(“ test”)。 这将返回一个元素列表-使用索引而不是不同的标签名称。

root.findall("test")[0].text --> "This is some text"
root.findall("test")[1].text --> "This is other text"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM