import lxml.html as PARSER
from lxml.html import fromstring
data = """<TextFormat>06</TextFormat>
<Text><![CDATA[<html><body><p>Ducdame was John Cowper Powys<p>other text</p></p></body></html>]]></Text>"""
root = PARSER.fromstring(data)
for ele in root.getiterator():
if ele.tag == 'text':
print ele.text_content()
这就是我现在得到的 - > Ducdame是John Cowper Powysother的文本。获取标签内的全部内容,包括html标签
但我需要“文本”标签中的全部内容。 这是我期待的结果。
<![CDATA[<html><body><p>Ducdame was John Cowper Powys<p>other text</p></p></body></html>]]>
我试过lxml,BeautifulSoup但没有得到我期待的结果。 我真的需要这个帮助。
由于
这不是工作,因为你的数据编码不正确。您不能将XML语法元素的字符串用作XML中的字符串。编码< and >为<和&gr;等,它将工作。 – Michael
其实这是从.onx文件格式输入的,但我不知道我应该如何解析它。所以我尝试使用lxml库。 但是,这正是我从我的输入文件中得到的输入。 –