【问题标题】:How to retrieve html from an xml?如何从 xml 中检索 html?
【发布时间】:2020-02-04 12:07:25
【问题描述】:

我正在尝试从 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 html xml xml-parsing html-parsing


【解决方案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>

【讨论】:

    猜你喜欢
    • 2011-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-30
    • 2010-11-25
    • 2023-03-05
    • 2020-06-12
    相关资源
    最近更新 更多