判断是不是sitemapindex, <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> 如果是sitemapindex则循环解析子xml,如果不是就直接解析xml文件 xml文件需要保留url和lasmod,使用python实现代码
时间: 2023-04-02 22:04:05 浏览: 83
这是一个sitemapindex,需要循环解析子xml。以下是使用Python实现的代码:
```python
import xml.etree.ElementTree as ET
def parse_sitemap(url):
tree = ET.parse(url)
root = tree.getroot()
if root.tag == '{http://www.sitemaps.org/schemas/sitemap/.9}sitemapindex':
for child in root:
parse_sitemap(child.find('{http://www.sitemaps.org/schemas/sitemap/.9}loc').text)
elif root.tag == '{http://www.sitemaps.org/schemas/sitemap/.9}urlset':
for child in root:
url = child.find('{http://www.sitemaps.org/schemas/sitemap/.9}loc').text
lastmod = child.find('{http://www.sitemaps.org/schemas/sitemap/.9}lastmod').text
# 处理url和lastmod
else:
# 不是sitemapindex也不是urlset,无法解析
pass
```
注意,这个代码只处理了sitemapindex和urlset两种情况,如果有其他类型的XML文件,需要根据实际情况进行修改。