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>
相关推荐
Geo_V7 分钟前
OpenAI 大模型 API 使用示例
python·chatgpt·openai·大模型应用·llm 开发
Hello_WOAIAI11 分钟前
2.4 python装饰器在 Web 框架和测试中的实战应用
开发语言·前端·python
搬山.摧城17 分钟前
线程池和单例模式
开发语言·单例模式
百锦再21 分钟前
第1章 Rust语言概述
java·开发语言·人工智能·python·rust·go·1024程序员节
一叶之秋141231 分钟前
QT背景介绍与环境搭建
开发语言·qt
tokepson33 分钟前
chatgpt-to-md优化并重新复习
python·ai·技术·pypi·记录
Victory_orsh41 分钟前
“自然搞懂”深度学习(基于Pytorch架构)——010203
人工智能·pytorch·python·深度学习·神经网络·算法·机器学习
java1234_小锋41 分钟前
PyTorch2 Python深度学习 - 模型保存与加载
开发语言·python·深度学习·pytorch2
Python图像识别44 分钟前
74_基于深度学习的垃圾桶垃圾溢出检测系统(yolo11、yolov8、yolov5+UI界面+Python项目源码+模型+标注好的数据集)
python·深度学习·yolo
MrSYJ1 小时前
可以指定 Jupyter Notebook 使用的虚拟环境吗
python·llm·agent