从网站的sitemap地图(sitemap.xml地图)里抓取url和lastmod成对放入列表里,python代码实现
时间: 2023-04-02 20:05:08 浏览: 117
可以使用Python中的xml.etree.ElementTree模块来解析sitemap.xml文件,然后提取其中的url和lastmod信息。具体代码如下:
```python
import xml.etree.ElementTree as ET
# 解析sitemap.xml文件
tree = ET.parse('sitemap.xml')
root = tree.getroot()
# 遍历所有的url标签,提取url和lastmod信息
url_list = []
for url in root.iter('{http://www.sitemaps.org/schemas/sitemap/.9}url'):
loc = url.find('{http://www.sitemaps.org/schemas/sitemap/.9}loc').text
lastmod = url.find('{http://www.sitemaps.org/schemas/sitemap/.9}lastmod').text
url_list.append((loc, lastmod))
# 打印结果
print(url_list)
```
这段代码会将sitemap.xml文件中所有的url和lastmod信息提取出来,并以元组的形式存储在url_list列表中。
相关问题
对网址https://www.u9seo.com/sitemap_index.xml进行解析,判断是不是sitemapindex,如果是则先解析子xml文件,再通过子xml文件抓取url和lastmod。如果不是sitemapindex,直接解析xml文件抓取url和lastmode,使用python实现
这是一个技术问题,我可以回答。根据网址https://www.u9seo.com/sitemap_index.xml的后缀名,可以判断它是一个sitemapindex文件。因此,需要先解析子xml文件,再通过子xml文件抓取url和lastmod。可以使用Python中的xml.etree.ElementTree模块来解析XML文件,具体实现可以参考以下代码:
```python
import requests
import xml.etree.ElementTree as ET
url = 'https://www.u9seo.com/sitemap_index.xml'
response = requests.get(url)
root = ET.fromstring(response.content)
if root.tag == '{http://www.sitemaps.org/schemas/sitemap/.9}sitemapindex':
for child in root:
sub_url = child.find('{http://www.sitemaps.org/schemas/sitemap/.9}loc').text
sub_response = requests.get(sub_url)
sub_root = ET.fromstring(sub_response.content)
for sub_child in sub_root:
url = sub_child.find('{http://www.sitemaps.org/schemas/sitemap/.9}loc').text
lastmod = sub_child.find('{http://www.sitemaps.org/schemas/sitemap/.9}lastmod').text
print(url, lastmod)
else:
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
print(url, lastmod)
```
这段代码会先判断是否是sitemapindex文件,如果是,则解析子xml文件,抓取其中的url和lastmod;如果不是,则直接解析xml文件,抓取其中的url和lastmod。
Python从sitemap里提取链接,如果是sitemap-index则抓取xml文件,再通过xml文件抓取url和时间,把url和时间放在一起,存入sqlite数据库,提供代码案例
您好,以下是Python从sitemap里提取链接并存入sqlite数据库的代码案例:
```python
import requests
import sqlite3
import xml.etree.ElementTree as ET
# 定义函数,从sitemap中提取url和时间并存入sqlite数据库
def extract_sitemap(sitemap_url, db_name):
# 请求sitemap文件
response = requests.get(sitemap_url)
# 如果是sitemap-index,则抓取xml文件
if response.content.startswith(b'<?xml'):
root = ET.fromstring(response.content)
# 遍历xml文件,抓取url和时间
for child in root:
sitemap_url = child[].text
extract_sitemap(sitemap_url, db_name)
# 如果是sitemap,则抓取url和时间
else:
root = ET.fromstring(response.content)
urls = []
for child in root:
url = child[].text
lastmod = child[1].text
urls.append((url, lastmod))
# 存入sqlite数据库
conn = sqlite3.connect(db_name)
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS urls
(url text, lastmod text)''')
c.executemany('INSERT INTO urls VALUES (?, ?)', urls)
conn.commit()
conn.close()
# 调用函数,提取url和时间并存入sqlite数据库
sitemap_url = 'https://www.example.com/sitemap.xml'
db_name = 'urls.db'
extract_sitemap(sitemap_url, db_name)
```
以上代码可以从指定的sitemap文件中提取url和时间,并存入sqlite数据库中。如果是sitemap-index,则会抓取其中的xml文件并提取其中的url和时间。