我有这个xml格式的网站地图。我怎样才能得到每一个<loc>
?在
<?xml version="1.0" encoding="UTF-8"?>
<urlset
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<!-- created with Free Online Sitemap Generator www.xml-sitemaps.com -->
<url>
<loc>https://www.nsnam.org/wiki/Main_Page</loc>
<lastmod>2018-10-24T03:03:05+00:00</lastmod>
<priority>1.00</priority>
</url>
<url>
<loc>https://www.nsnam.org/wiki/Current_Development</loc>
<lastmod>2018-10-24T03:03:05+00:00</lastmod>
<priority>0.80</priority>
</url>
<url>
<loc>https://www.nsnam.org/wiki/Developer_FAQ</loc>
<lastmod>2018-10-24T03:03:05+00:00</lastmod>
<priority>0.80</priority>
</url>
程序看起来像这样。在
^{2}$
我建议您使用elementtree标准库包:
文档链接:
更新:
.iter()
而不是.findall()
/.find()
来直接获得loc
元素。这可能是好的,也可能不好,这取决于XML的结构和用例。在你的代码在我这方面运行得很好。您只需在
url
和loc
之前添加:{http://www.sitemaps.org/schemas/sitemap/0.9}
这里:
相关问题 更多 >
编程相关推荐