Python:使用sitemap库生成网站地图文件sitemap.xml

可以使用sitemap库生成网站地图文件sitemap.xml

文档

安装

复制代码
pip install sitemap

Urlset

python 复制代码
from sitemap import Url, Urlset
from datetime import datetime

urlset = Urlset()

url = Url(
    loc='https://www.example.com/',
    lastmod=datetime.now(),
    changefreq='weekly'
)

urlset.add_url(url)

# urlset.to_string()
urlset.write_xml('sitemap.xml')

生成的文件:sitemap.xml

xml 复制代码
<?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">
    <url>
        <loc>https://www.example.com/</loc>
        <lastmod>2024-07-23</lastmod>
        <changefreq>weekly</changefreq>
    </url>
</urlset>

Siteindex

python 复制代码
from datetime import datetime

from sitemap import Sitemap, Siteindex

siteindex = Siteindex()

sitemap = Sitemap(
    loc='https://www.example.com/sitemap.xml',
    lastmod=datetime.now()
)

siteindex.add_sitemap(sitemap)

# siteindex.to_string()
siteindex.write_xml('sitemap.xml')
xml 复制代码
<?xml version='1.0' encoding='utf-8'?>
<siteindex 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/siteindex.xsd">
    <sitemap>
        <loc>https://www.example.com/sitemap.xml</loc>
        <lastmod>2024-07-23</lastmod>
    </sitemap>
</siteindex>
相关推荐
虾球xz40 分钟前
CppCon 2015 学习:Reactive Stream Processing in Industrial IoT using DDS and Rx
开发语言·c++·物联网·学习
工业互联网专业1 小时前
基于django+vue的健身房管理系统-vue
vue.js·python·django·毕业设计·源码·课程设计·健身房管理系统
aischang1 小时前
统信桌面专业版如何使用python开发平台jupyter
开发语言·python·jupyter·统信uos
红鼻子时代1 小时前
Django RBAC项目后端实战 - 03 DRF权限控制实现
后端·python·django·rabc
敲键盘的小夜猫1 小时前
大模型链路调试平台之LangSmith实战指南
python·langchain
狐凄1 小时前
Python实例题:Python计算概率论
开发语言·python·概率论
Y3174291 小时前
python Day46 学习(日志Day15复习)
python·学习·机器学习
这里有鱼汤1 小时前
一文读懂量化交易中最常用的5种均线,附源码,建议收藏
后端·python
学不会就看2 小时前
selenium学习实战【Python爬虫】
python·学习·selenium
q567315232 小时前
分布式增量爬虫实现方案
开发语言·分布式·爬虫·python