python - 用 Python 解析这个 XML 站点地图的最有效方法是什么?
问题描述
我有以下要解析的站点地图:
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://www.example.com/examplea</loc>
<priority>0.5</priority>
<lastmod>2019-03-14</lastmod>
<changefreq>daily</changefreq>
</url>
<url>
<loc>https://www.example.com/exampleb</loc>
<priority>0.5</priority>
<lastmod>2019-03-14</lastmod>
<changefreq>daily</changefreq>
</url>
</urlset>
使用 Python 获取 loc 标记中的 url 链接的最快方法是什么?
我尝试使用 ElementTree,但我认为它因为命名空间而不起作用。
我需要得到“ https://www.example.com/examplea ”和“ https://www.example.com/exampleab ”
解决方案
import re
str = """
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://www.example.com/examplea</loc>
<priority>0.5</priority>
<lastmod>2019-03-14</lastmod>
<changefreq>daily</changefreq>
</url>
<url>
<loc>https://www.example.com/exampleb</loc>
<priority>0.5</priority>
<lastmod>2019-03-14</lastmod>
<changefreq>daily</changefreq>
</url>
</urlset>
"""
url = re.findall("<loc>(.*?)</loc>", str)
推荐阅读
- objective-c - 如果在 macOS Catalina 中的屏幕边缘附近打开,NSAlert 不可见
- tensorflow2.0 - 推理时如何在tensorflow 2.0中使用tfa.seq2seq.dynamic_decode
- javascript - 无法从另一个组件打开模态组件
- c# - 如何首先将对象旋转到 30,然后在相反的方向旋转到 -30
- angular - 根据角度日期过滤数组中的数据
- laravel - Laravel Redis 删除所有以字符串开头的键
- javascript - 检查是否从服务文件 Angular 7 成功获得响应
- mysql - NodeJS:从多语句 MySQL 查询的结果中获取值
- reactjs - 在 ReactJS 应用程序中使用 setState 函数的问题
- arrays - 获取数组中数字的平均值,即哈希值