最简单可靠的方式是用Python标准库xml.etree.ElementTree手动构造符合Sitemap协议的XML,根节点为urlset,每个url包含必填loc及可选lastmod、changefreq、priority,确保loc为绝对URL、lastmod为ISO 8601格式。

用Python生成 sitemap.xml 最简单可靠的方式是手动构造符合Sitemap协议的XML内容,或借助轻量库(如 xml.etree.ElementTree 或第三方库 django-sitemaps / flask-sitemap)。对大多数静态网站或小型动态站,纯Python + 标准库就足够,无需引入复杂框架。
使用 xml.etree.ElementTree 构建标准 sitemap.xml
Python 标准库中的 xml.etree.ElementTree 足以生成合法、可被搜索引擎识别的 sitemap。关键点:根节点为 urlset,每个 url 包含 loc(必填),可选 lastmod、changefreq、priority。
- 确保
loc是完整、可访问的绝对 URL(如https://www.php.cn/link/e4639aefe47ac53c3df3d8f9846b5161blog/post1/) -
lastmod格式必须为YYYY-MM-DD或YYYY-MM-DDThh:mm:ss+00:00(推荐 ISO 8601) - 避免特殊字符未转义——
ElementTree会自动处理,但手动拼接字符串时需用xml.sax.saxutils.escape()
一个可直接运行的生成脚本示例
以下脚本生成包含 3 个页面的 sitemap.xml,保存到当前目录:
import xml.etree.ElementTree as ET
from datetime import datetime
<h1>创建根元素</h1><p>urlset = ET.Element("urlset", xmlns="<a href="https://www.php.cn/link/654f3a10edb3bb1755a43cc4f9be9dc6">https://www.php.cn/link/654f3a10edb3bb1755a43cc4f9be9dc6</a>")</p><p><span>立即学习</span>“<a href="https://pan.quark.cn/s/00968c3c2c15" style="text-decoration: underline !important; color: blue; font-weight: bolder;" rel="nofollow" target="_blank">Python免费学习笔记(深入)</a>”;</p><div class="aritcle_card flexRow">
<div class="artcardd flexRow">
<a class="aritcle_card_img" href="/ai/2663" title="靠岸学术"><img
src="https://img.php.cn/upload/ai_manual/001/503/042/69b3bf2c510bf638.jpeg" alt="靠岸学术" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a>
<div class="aritcle_card_info flexColumn">
<a href="/ai/2663" title="靠岸学术">靠岸学术</a>
<p>一款集翻译,阅读,文献管理于一体的英文文献阅读器</p>
</div>
<a href="/ai/2663" title="靠岸学术" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span> </a>
</div>
</div><h1>定义页面数据(实际项目中可从数据库、文件列表或 CMS API 获取)</h1><p>pages = [
{"loc": "<a href="https://www.php.cn/link/e4639aefe47ac53c3df3d8f9846b5161">https://www.php.cn/link/e4639aefe47ac53c3df3d8f9846b5161</a>", "lastmod": "2024-05-01", "changefreq": "weekly", "priority": "1.0"},
{"loc": "<a href="https://www.php.cn/link/e4639aefe47ac53c3df3d8f9846b5161about/">https://www.php.cn/link/e4639aefe47ac53c3df3d8f9846b5161about/</a>", "lastmod": "2024-04-22", "changefreq": "monthly", "priority": "0.8"},
{"loc": "<a href="https://www.php.cn/link/e4639aefe47ac53c3df3d8f9846b5161blog/">https://www.php.cn/link/e4639aefe47ac53c3df3d8f9846b5161blog/</a>", "lastmod": "2024-05-10", "changefreq": "daily", "priority": "0.9"},
]</p><p>for page in pages:
url = ET.SubElement(urlset, "url")
ET.SubElement(url, "loc").text = page["loc"]
ET.SubElement(url, "lastmod").text = page["lastmod"]
ET.SubElement(url, "changefreq").text = page["changefreq"]
ET.SubElement(url, "priority").text = page["priority"]</p><h1>写入文件(缩进需手动处理,或用第三方库如 xmltodict / lxml 美化)</h1><p>tree = ET.ElementTree(urlset)
tree.write("sitemap.xml", encoding="utf-8", xml_declaration=True)</p><p>print("✅ sitemap.xml 已生成")
运行后得到结构清晰、合规的 XML 文件,可直接部署到网站根目录。
处理大型网站:分片与索引(sitemap index)
单个 sitemap 最多支持 5 万条 URL,总大小不超过 50MB(压缩后)。超限时需拆分为多个 sitemap 并生成 sitemap_index.xml:
- 将 URL 列表按每 4 万条一组切分
- 为每组生成独立 sitemap(如
sitemap-1.xml,sitemap-2.xml) - 用
sitemapindex根节点汇总所有子 sitemap 的loc和可选lastmod - 在 robots.txt 中添加
Sitemap: https://www.php.cn/link/e4639aefe47ac53c3df3d8f9846b5161sitemap_index.xml
自动化建议:集成到构建流程
不推荐手动生成。应将其嵌入网站构建环节:
- 静态站点(如 Jekyll、Hugo、MkDocs):用插件或自定义脚本,在
build后自动生成 - Flask/Django:在管理命令或部署钩子中调用生成函数(Django 可直接用
django.contrib.sitemaps) - CI/CD(如 GitHub Actions):添加 Python 步骤,每次 push 后更新并提交
sitemap.xml
保持 sitemap 与线上内容实时一致,比“一次性生成”更重要。









