[英]How to extract text in nested xml after closing of one tag in python using xml.etree.ElementTree
我想提取xml文檔中的所有文本,並且在以下情況下出現問題:
...
<a>
hello
<B>
there
</B>
How was your day.
.....
</a>
在此代碼段中,我可以獲取文本“ hello”和“ there”,因為可以使用以下標記獲取它們:
a.text
b.text
但我不知道如何訪問“您今天過的怎么樣”。 部分。
您正在尋找元素的.tail
屬性 :
>>> from xml.etree import ElementTree
>>> example = ElementTree.fromstring('''\
... <a>
... hello
... <B>
... there
... </B>
... How was your day.
... </a>
... '''
... )
>>> example
<Element 'a' at 0x10715d150>
>>> example.text
'\nhello\n'
>>> example.find('B')
<Element 'B' at 0x10715d7d0>
>>> example.find('B').text
'\nthere\n'
>>> example.find('B').tail
'\nHow was your day.\n'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.