![](/img/trans.png)
[英]how to get a specific tag text from a similar type of tag in XML in python?
[英]python Parsing xml: get text from tag which contains <i> or <b> or similar
我正在使用 xml.etree.ElementTree。 当我尝试从 AbstractText 获取文本时,如果文本中有像 i、b 或类似标签这样的格式标签,我会得到 None 或部分文本。
这是一个xml示例
<root>
<AbstractText><b>1.</b> test text <b> 2. </b> is very silly.</AbstractText>
<AbstractText>hello <b> this is </b> another example </AbstractText>
</root>
python代码是
tree = ET.parse("xml/test.xml")
root =tree.getroot()
for node in root.findall('AbstractText'):
print(node.text)
输出是
None
hello
我该如何解决? 我想要所有没有 i、b 或其他信息的文本
这里有一个例子
import xml.etree.ElementTree as ET
from lxml import etree
tree = etree.parse('file.xml')
for node in tree.findall('AbstractText'):
#print(node)
print(etree.tostring(node, encoding='utf8', method='text'))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.