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

畫卷琴夢
发布: 2025-12-18 09:24:07
原创
619人浏览过
最简单可靠的方式是用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><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">
                        <a class="aritcle_card_img" href="/xiazai/code/10761">
                            <img src="https://img.php.cn/upload/webcode/000/000/010/176362920210614.jpg" alt="通达CMS蓝色中英双语企业网站1.4.2">
                        </a>
                        <div class="aritcle_card_info">
                            <a href="/xiazai/code/10761">通达CMS蓝色中英双语企业网站1.4.2</a>
                            <p>通达CMS是采用PHP+MYSQL进行开发的。支持伪静态设置,可生成google和百度地图,支持自定义url、关键字和描述,利于收录...后台简单明了,代码简洁,采用DIV+CSS 利于SEO,企业建站系统是一套专门用于中小企业网站建设的网站管理系统。</p>
                            <div class="">
                                <img src="/static/images/card_xiazai.png" alt="通达CMS蓝色中英双语企业网站1.4.2">
                                <span>0</span>
                            </div>
                        </div>
                        <a href="/xiazai/code/10761" class="aritcle_card_btn">
                            <span>查看详情</span>
                            <img src="/static/images/cardxiayige-3.png" alt="通达CMS蓝色中英双语企业网站1.4.2">
                        </a>
                    </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 与线上内容实时一致,比“一次性生成”更重要。

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

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号