如何用Python生成sitemap.xml网站地图

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

如何用python生成sitemap.xml网站地图

用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(必填),可选 lastmodchangefreqpriority

  • 确保 loc 是完整、可访问的绝对 URL(如 https://www.php.cn/link/e4639aefe47ac53c3df3d8f9846b5161blog/post1/
  • lastmod 格式必须为 YYYY-MM-DDYYYY-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><h1>定义页面数据(实际项目中可从数据库、文件列表或 CMS API 获取)</h1><p>pages = [
{"loc": "<a href="https://www.php.cn/link/e4639aefe47ac53c3df3d8f9846b5161">https://www.php.cn/link/e4639aefe47ac53c3df3d8f9846b5161</a>", "lastmod": "2025-05-01", "changefreq": "weekly", "priority": "1.0"},
{"loc": "<a href="https://www.php.cn/link/e4639aefe47ac53c3df3d8f9846b5161about/">https://www.php.cn/link/e4639aefe47ac53c3df3d8f9846b5161about/</a>", "lastmod": "2025-04-22", "changefreq": "monthly", "priority": "0.8"},
{"loc": "<a href="https://www.php.cn/link/e4639aefe47ac53c3df3d8f9846b5161blog/">https://www.php.cn/link/e4639aefe47ac53c3df3d8f9846b5161blog/</a>", "lastmod": "2025-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 文件,可直接部署到网站根目录。

通达CMS蓝色中英双语企业网站1.4.2 通达CMS蓝色中英双语企业网站1.4.2

通达CMS是采用PHP+MYSQL进行开发的。支持伪静态设置,可生成google和百度地图,支持自定义url、关键字和描述,利于收录...后台简单明了,代码简洁,采用DIV+CSS 利于SEO,企业建站系统是一套专门用于中小企业网站建设的网站管理系统。

通达CMS蓝色中英双语企业网站1.4.2 0 查看详情 通达CMS蓝色中英双语企业网站1.4.2

处理大型网站:分片与索引(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 与线上内容实时一致,比“一次性生成”更重要。

以上就是如何用Python生成sitemap.xml网站地图的详细内容,更多请关注其它相关文章!

本文转自网络,如有侵权请联系客服删除。