![](/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.