Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python lxml更改标记层次结构?_Python_Html_Xml_Lxml - Fatal编程技术网

Python lxml更改标记层次结构?

Python lxml更改标记层次结构?,python,html,xml,lxml,Python,Html,Xml,Lxml,我对lxml有一个小问题。我正在将XML文档转换为HTML文档。 原始XML如下所示(看起来像HTML,但在XML文档中): 我明白了: <div><p>Localization - Eiffel tower? Paris or Vegas </p><p>Bayes theorem p(A|B)</p></div> 本地化-埃菲尔铁塔?巴黎或拉斯维加斯贝叶斯定理p(A | B) 我对s没有任何问题,但是“贝叶斯定理”段落

我对lxml有一个小问题。我正在将XML文档转换为HTML文档。 原始XML如下所示(看起来像HTML,但在XML文档中):

我明白了:

<div><p>Localization - Eiffel tower? Paris or Vegas </p><p>Bayes theorem p(A|B)</p></div>
本地化-埃菲尔铁塔?巴黎或拉斯维加斯

贝叶斯定理p(A | B)

我对s没有任何问题,但是“贝叶斯定理”段落不再嵌套在外部段落中这一事实是一个问题


有人知道lxml为什么这样做,以及如何阻止它吗?谢谢。

lxml之所以这样做,是因为它不存储无效的HTML,也不存储HTML中的
元素:

p元素表示一个段落。它不能包含块级元素(包括P本身)


您使用的是lxml的HTML解析器,而不是XML解析器。请尝试以下方法:

>>> from lxml import etree
>>> item = '<p>Eiffel tower? Paris or Vegas <p>Bayes theorem p(A|B)</p></p>'
>>> root = etree.fromstring(item)
>>> etree.tostring(root, pretty_print=True)
'<p>Eiffel tower? Paris or Vegas <p>Bayes theorem p(A|B)</p></p>\n'
来自lxml导入etree的
>>
>>>项目='埃菲尔铁塔?巴黎或维加斯贝叶斯定理p(A | B)

' >>>root=etree.fromstring(项) >>>etree.tostring(root,pretty\u print=True) “埃菲尔铁塔?巴黎或拉斯维加斯贝叶斯定理p(A | B)

\n'
嗯。这是我不知道的。谢谢
<div><p>Localization - Eiffel tower? Paris or Vegas </p><p>Bayes theorem p(A|B)</p></div>
>>> from lxml import etree
>>> item = '<p>Eiffel tower? Paris or Vegas <p>Bayes theorem p(A|B)</p></p>'
>>> root = etree.fromstring(item)
>>> etree.tostring(root, pretty_print=True)
'<p>Eiffel tower? Paris or Vegas <p>Bayes theorem p(A|B)</p></p>\n'