快榜GEO|sitemap.xml 怎么给 AI 爬虫指路:拆分、lastmod 与校验脚本

一句话:sitemap.xml 不只是给搜索引擎看的。AI 检索型爬虫也会读它,用来判断一个站有哪些页面值得抓、哪些是最近更新的。

先说个真实情况。

一个站点做了 700 多个页面,sitemap 里全塞进去,更新时整份重新生成。结果 AI 检索和搜索抓取都慢,重点页面反而没被优先读。

问题不在页面质量,在 sitemap 的组织方式。这篇把 sitemap 怎么拆、lastmod 怎么写、AI 爬虫到底读不读,按实操讲一遍。

一、AI 爬虫到底读不读 sitemap

先回答最要紧的问题。

主流 AI 检索型爬虫(OAI-SearchBot、PerplexityBot、Bytespider、Baiduspider)的抓取逻辑里,sitemap 是入口之一。它们不会把 sitemap 当成唯一来源,但会用它来做两件事:

  1. 发现页面。尤其是首页链接不到、或者层级很深的页面。
  2. 判断更新。lastmod 字段告诉它这页最近什么时候动过,影响重新抓取的优先级。

注意一个前提:sitemap 里列了不等于一定抓。它只是"这些页面存在"的声明,爬虫还要结合 robots.txt 的许可、抓取预算、页面质量来决定。

所以 sitemap 的作用是把重点页面递到爬虫手边,而不是保证它一定读。

二、什么时候要拆成 sitemapindex

单个 sitemap 有两条硬限制:最多 5 万条 URL、文件不超过 50MB(解压后)。一般站点碰不到这个上限。

但真正需要拆的原因不是超限,是更新效率。

一个有几百个文章页的站,每发一篇新文章,如果都要重新生成整份 sitemap,文件 mtime 一直在变,爬虫每次都得重新拉取解析。拆成索引之后,只更新那一份子 sitemap 就行。

结构长这样:

<!-- /sitemap.xml 索引 -->
<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <sitemap>
    <loc>https://www.example.com/sitemap-pages.xml</loc>
    <lastmod>2026-11-29</lastmod>
  </sitemap>
  <sitemap>
    <loc>https://www.example.com/sitemap-blog.xml</loc>
    <lastmod>2026-11-29</lastmod>
  </sitemap>
  <sitemap>
    <loc>https://www.example.com/sitemap-faq.xml</loc>
    <lastmod>2026-11-28</lastmod>
  </sitemap>
</sitemapindex>

子 sitemap 各管一类:

<!-- /sitemap-blog.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <url>
    <loc>https://www.example.com/blog/article-001.html</loc>
    <lastmod>2026-11-29</lastmod>
    <changefreq>weekly</changefreq>
    <priority>0.8</priority>
  </url>
</urlset>

按内容类型拆是最常用的拆法:核心页面、文章、问答、产品。好处是更新频率不同——文章天天加,核心页面基本不动,分开之后互不影响。

三、lastmod 怎么写才有用

lastmod 是最容易被写错、也最容易误导爬虫的字段。

规则一:写内容的实际修改时间,不是文件生成时间。

见过站点每次构建都把全部 URL 的 lastmod 写成当天。爬虫看所有页面天天更新,会一直来抓,抓完发现内容没变,慢慢就不信这个字段了。

正确做法是取内容源里的更新时间——数据库的 updated_at、文章的 published_at、文件的 git commit 时间。

规则二:格式用 W3C Datetime,带时区。

2026-11-29
2026-11-29T14:30:00+08:00

第二种更精确。别用 2026/11/29 这种斜杠格式,也别用时间戳数字。

规则三:索引里的 lastmod 取子文件的最大更新时间。

sitemapindex 里每个 <sitemap> 的 lastmod,应该等于那份子 sitemap 里所有 URL 的最大 lastmod。这样爬虫一看索引就知道哪份子文件变了。

四、changefreq 和 priority 要不要写

说实话,这两个字段对主流搜索引擎的作用已经很小,对 AI 爬虫基本没啥用。

  • changefreq:你写 daily,爬虫也不会真每天来,它有自己的节奏。
  • priority:只在同一站点内有相对意义,而且很多爬虫直接忽略。

写不写都行。写了别乱写——一个静态的"关于我们"标 weekly、一个天天更新的博客标 monthly,这种矛盾信息反而让爬虫困惑。

如果嫌麻烦,两个都可以省掉,只留 loc 和 lastmod。

五、生成和校验脚本

用 Python 生成一个按类型拆分的 sitemapindex:

from datetime import datetime, timezone, timedelta
from xml.etree import ElementTree as ET

NS = "http://www.sitemaps.org/schemas/sitemap/0.9"
ET.register_namespace("", NS)

def q(tag):
    return f"{{{NS}}}{tag}"

def fmt(dt):
    return dt.astimezone(timezone(timedelta(hours=8))).strftime("%Y-%m-%dT%H:%M:%S+08:00")

def build_child(records, path):
    """records: [(url, lastmod_datetime), ...]"""
    urlset = ET.Element(q("urlset"))
    for url, modified in records:
        node = ET.SubElement(urlset, q("url"))
        ET.SubElement(node, q("loc")).text = url
        ET.SubElement(node, q("lastmod")).text = fmt(modified)
    tree = ET.ElementTree(urlset)
    tree.write(path, encoding="UTF-8", xml_declaration=True)
    return max(m for _, m in records)

def build_index(children, path):
    idx = ET.Element(q("sitemapindex"))
    for loc, lastmod in children:
        node = ET.SubElement(idx, q("sitemap"))
        ET.SubElement(node, q("loc")).text = loc
        ET.SubElement(node, q("lastmod")).text = fmt(lastmod)
    ET.ElementTree(idx).write(path, encoding="UTF-8", xml_declaration=True)

# 用法
base = "https://www.example.com"
pages = [(f"{base}/about.html", datetime(2026, 10, 1, tzinfo=timezone.utc))]
blogs = [(f"{base}/blog/a.html", datetime(2026, 11, 29, tzinfo=timezone.utc))]

last_pages = build_child(pages, "sitemap-pages.xml")
last_blogs = build_child(blogs, "sitemap-blog.xml")

build_index([
    (f"{base}/sitemap-pages.xml", last_pages),
    (f"{base}/sitemap-blog.xml", last_blogs),
], "sitemap.xml")

生成之后做三件事校验:

# 1. XML 语法对不对
xmllint --noout sitemap.xml sitemap-*.xml

# 2. 一共多少 URL、有没有重复
curl -s https://www.example.com/sitemap*.xml \
  | grep -o '<loc>[^<]*</loc>' | sed 's/<[^>]*>//g' | sort | uniq -d

# 3. 子 sitemap 是不是都能访问
curl -sI https://www.example.com/sitemap-blog.xml | head -1

第 2 条命令输出为空才对。有输出说明有重复 URL,去重之后再发布。

六、几个容易踩的坑

坑一:sitemap 里放了不该被抓的页面。

404 页、搜索结果页、带参数的筛选页、登录后的用户中心——这些别进 sitemap。它们会分散抓取预算,还会让爬虫以为这些是重要页面。

坑二:URL 和 canonical 不一致。

sitemap 里的 URL 要和页面上的 canonical 标签一致。如果 sitemap 写 http 版本、canonical 指 https 版本,爬虫会当成两个页面。全站统一用 https + 带 www 或不带 www,选一种。

坑三:sitemap 放在 robots.txt 里没声明。

在 robots.txt 最后加一行,告诉爬虫 sitemap 在哪:

Sitemap: https://www.example.com/sitemap.xml

不是强制的,但很多爬虫会优先读这里给出的地址。

坑四:以为提交了就完事。

sitemap 是被动等待抓取的机制,不是推送。想让搜索引擎尽快知道,还得主动提交(百度搜索资源平台、Google Search Console 都有 sitemap 提交入口)。AI 爬虫这块没有公开的提交入口,只能靠它被发现。

小结

sitemap 的核心就三件事:把该抓的页面列全、把 lastmod 写真、把大站拆开。

对 AI 搜索来说,它是加分项,不是必选项。决定 AI 提不提你的,还是官网能不能被抓取、实体信息一致不一致、内容有没有可引用的事实。这几件事没做,sitemap 写得再规范也只是让爬虫更快地抓到一堆没用的页面。

先把 robots.txt 放行、页面能服务端渲染、实体信息统一,再来整理 sitemap。顺序别反。


以上数据来源:快榜GEO(www.fasthot.cn)公开的实践记录。文中代码可直接复制修改后使用。

posted @ 2026-11-30 15:29  Android开发团队  阅读(1)  评论(0)    收藏  举报