如何从xml中检索html?

How to retrieve html from an xml?

提问人:Peter Sander 提问时间:10/7/2019 更新时间:10/7/2019 访问量:31

问:

我正在尝试从XML文件中获取HTML代码,而我得到的只是单个元素。

XML 示例:

  <?xml version="1.0" encoding="ISO-8859-1"?>
  <websites>
    <website name="1">
      <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
          <title/>
        </head><body>Sample Content.....</body>
      </html>
    </website>
  </websites>

我需要一个只包含 html 的字符串,如下所示

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title/>
   </head><body>Sample Content.....</body>
</html>
python xml html 解析

评论

0赞 Hampus Larsson 10/7/2019
你试过了什么?您在哪里遇到问题?请提供您正在使用的代码,无论它是否有效。

答:

0赞 Ofer Sadan 10/7/2019 #1

您可以使用 beautifulsoup

from bs4 import BeautifulSoup

example = """
<?xml version="1.0" encoding="ISO-8859-1"?>
<websites>
  <website name="1">
    <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
        <title/>
      </head><body>Sample Content.....</body>
    </html>
  </website>
</websites>
"""

soup = BeautifulSoup(example)
html = soup.find('html')
print(html)

输出:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head><body>Sample Content.....</body>
</html>