如何从xml检索html?

问题描述 投票:0回答:1

我正在尝试从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个回答
0
投票
您可以使用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)

© www.soinside.com 2019 - 2024. All rights reserved.