Python 如何在ElementTree元素中表示多个文本部分?
我正在使用ElementTree来处理一些html。我认为html是一种xml语言,所以这应该可以 在html中,文本中可以包含标记:Python 如何在ElementTree元素中表示多个文本部分?,python,python-2.7,elementtree,Python,Python 2.7,Elementtree,我正在使用ElementTree来处理一些html。我认为html是一种xml语言,所以这应该可以 在html中,文本中可以包含标记: <p> This paragraph <em>has some</em> emphasised words. </p> 这一段有一些强调的词语。 所以“p”元素有一些文本(“本段”)、一个子元素(“em”)和更多的文本(“强调词”) 但是ElementTree元素有一个文本属性,即字符串。子元素在一个列表中
<p>
This paragraph <em>has some</em> emphasised words.
</p>
这一段有一些强调的词语。
所以“p”元素有一些文本(“本段”)、一个子元素(“em”)和更多的文本(“强调词”)
但是ElementTree元素有一个文本属性,即字符串。子元素在一个列表中,但文本都在一个字符串中
如何在ElementTree中表示此html?可能吗?您正在尝试解析它吗
import xml.etree.ElementTree as ET
def processElem(elem):
if elem.text is not None:
print elem.text
for child in elem:
processElem(child)
if child.tail is not None:
print child.tail
xml = '''<p>
This paragraph <em>has some</em> emphasised words.
</p>'''
root = ET.fromstring(xml)
processElem(root)
或者您正在尝试修改HTML
from xml.etree.ElementTree import Element, SubElement, tostring
top = Element('p')
top.text = 'This paragraph '
child_with_tail = SubElement(top, 'em')
child_with_tail.text = 'has some'
child_with_tail.tail = ' emphasised words.'
print tostring(top)
给出:
This paragraph
has some
emphasised words.
<p>This paragraph <em>has some</em> emphasised words.</p>
这一段有一些重点词语
Ahhh,那么您是说每个嵌入元素后面的文本,直到下一个嵌入元素,都位于嵌入元素的尾部?是的,这是正确的。看见