python - 如何从 xml 中检索 html?
问题描述
我正在尝试从 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>
解决方案
您可以使用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>
推荐阅读
- selenium - 使用量角器和黄瓜运行多个实例时出错
- javascript - JavaScript .on() 方法是如何定义的?
- python - 减少具有许多重叠点和 alpha 的矢量化散点图磁盘上的大小
- c# - 如何使用httpclient c#将文件上传到ASANA api中的任务
- jquery - 使用 MutationObserver 使 jQuery 识别新按钮的属性(通过 ajax 插入)
- php - 刀片中的Laravel资源匹配
- solr - 创建 SolrCore 'sitecore_core_index' 时出错:缺少非传统模式 coreNodeName
- php - 仅在管理员模式下检测到 Windows PHP 扩展
- python - 无法在 python3 中使用 bs4 解析包含“.html#/something”的地址
- java - web.xml 中的上下文参数值被忽略