【发布时间】:2014-02-18 11:21:28
【问题描述】:
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 的文本。
但我需要“文本”标签中的全部内容。 这是我期待的结果。
<![CDATA[<html><body><p>Ducdame was John Cowper Powys<p>other text</p></p></body></html>]]>
我尝试了 lxml,BeautifulSoup,但没有得到我期望的结果。 我真的需要这个帮助。
谢谢
【问题讨论】:
-
它不起作用,因为您的数据未正确编码。您不能将带有 XML 语法元素的字符串用作 XML 内部的字符串。将 编码为 <和&gr;等等,它会工作的。
-
实际上先生这个输入来自 .onx 文件格式,但我不知道我应该如何解析它。所以我尝试使用 lxml 库。但这正是我从输入文件中得到的输入。
标签: python