春江暮客

春江暮客的个人学习分享网站

2026 站长实战:用 llms.txt + Python 自动同步 AI 搜索入口

2026-05-24 技术

最近明显感觉到一个变化:很多流量不再只来自传统搜索,而是来自 AI 搜索、AI 问答和各类带总结能力的工具。

问题是,很多站点即使内容不错,也经常遇到这三个现实情况:

  1. AI 系统发现页面慢
  2. 抓到页面但抓不到重点
  3. 新文章上线后,入口文件没有及时更新

如果你正在维护内容站,llms.txt 值得尽快补上。本文给你一套可以直接落地的流程:

  1. 先手写一个可用版 llms.txt
  2. 再用 Python 自动生成,避免后期维护成本
  3. 发布后做快速验证,确保真实可抓

什么是 llms.txt

可以把 llms.txt 理解为“给 AI 读取器看的站点导航页”。

它通常放在网站根目录,例如:

  • https://www.bobobk.com/llms.txt

它不是标准 SEO 协议的替代品,也不会替代 sitemap.xmlrobots.txt。更实用的定位是:

  • 把你希望 AI 优先理解的内容入口集中写清楚
  • 给出主题、分类和高价值链接
  • 降低 AI 系统抓取时的路径成本

方法 1:先手写一个最小可用版本

先不要追求复杂,先让它可用。

新建 static/llms.txt,内容示例:

# Bobobk

> Practical tutorials on Python, Linux, SEO automation, and data tools.

## Core Sections
- Blog (CN): https://www.bobobk.com/
- Blog (EN): https://www.bobobk.com/en/
- Latest posts: https://www.bobobk.com/index.xml

## High-value guides
- https://www.bobobk.com/how-to-improve-index-speed-by-indexnow.html
- https://www.bobobk.com/python-wordpress-workflow.html
- https://www.bobobk.com/build_own_tron_wallet.html
- https://www.bobobk.com/build_own_solana_wallet.html

这样 Hugo 构建后会自动发布到根目录。

方法 2:用 Python 自动生成 llms.txt

手写最大的问题是:文章一多就会漏更新。

下面这个脚本做三件事:

  1. 读取 public/index.xml
  2. 按最近更新时间筛选最新文章
  3. 自动输出 static/llms.txt

1. 准备环境

这个版本使用 Python 标准库解析 RSS,无需额外安装依赖。

2. 脚本代码

#!/usr/bin/env python3
from __future__ import annotations

from datetime import datetime
from email.utils import parsedate_to_datetime
from pathlib import Path
import xml.etree.ElementTree as ET

SITE_NAME = "Bobobk"
SITE_DESC = "Practical tutorials on Python, Linux, SEO automation, and data tools."
SITE_CN = "https://www.bobobk.com/"
SITE_EN = "https://www.bobobk.com/en/"
RSS_PATH = Path("public/index.xml")
RSS_URL = "https://www.bobobk.com/index.xml"
OUTPUT = Path("static/llms.txt")
TOP_N = 20


def parse_pub_date(value: str | None) -> datetime:
    if not value:
        return datetime.min
    try:
        return parsedate_to_datetime(value)
    except Exception:
        return datetime.min


def text_of(parent: ET.Element, tag: str, default: str = "") -> str:
    node = parent.find(tag)
    if node is None or node.text is None:
        return default
    return node.text.strip()


def read_items_from_rss(path: Path) -> list[dict[str, str | datetime]]:
    if not path.exists():
        return []

    root = ET.parse(path).getroot()
    channel = root.find("channel")
    if channel is None:
        return []

    items: list[dict[str, str | datetime]] = []
    for item in channel.findall("item"):
        title = text_of(item, "title", "Untitled")
        link = text_of(item, "link", "")
        pub_date = parse_pub_date(text_of(item, "pubDate", ""))
        if link:
            items.append({"title": title, "link": link, "pub_date": pub_date})

    items.sort(key=lambda x: x["pub_date"], reverse=True)
    return items


def build_llms_text(items: list[dict[str, str | datetime]]) -> str:
    lines = [
        f"# {SITE_NAME}",
        "",
        f"> {SITE_DESC}",
        "",
        "## Core Sections",
        f"- Blog (CN): {SITE_CN}",
        f"- Blog (EN): {SITE_EN}",
        f"- Latest posts: {RSS_URL}",
        "",
        "## Latest High-value Posts",
    ]

    for item in items[:TOP_N]:
        title = str(item["title"]).replace("\n", " ").strip()
        link = str(item["link"]).strip()
        lines.append(f"- {title}: {link}")

    lines.append("")
    return "\n".join(lines)


def main() -> None:
    items = read_items_from_rss(RSS_PATH)
    content = build_llms_text(items)

    OUTPUT.parent.mkdir(parents=True, exist_ok=True)
    OUTPUT.write_text(content, encoding="utf-8")

    print(f"Generated {OUTPUT} with {min(len(items), TOP_N)} links")


if __name__ == "__main__":
    main()

3. 执行命令

# 先构建,确保 public/index.xml 是最新
hugo --config hugotest.toml -d public/

# 生成 llms.txt
python3 scripts/generate_llms_txt.py

# 再构建一次,把 static/llms.txt 发布到站点根目录
hugo --config hugotest.toml -d public/

上线后如何验证

1. 先看文件是否可访问

curl -I https://www.bobobk.com/llms.txt
curl https://www.bobobk.com/llms.txt | head -n 30

预期:

  • 返回状态码 200
  • 内容包含最近文章链接

2. 联动 IndexNow(可选)

如果你已经在用 IndexNow,可以把 llms.txt 一起推送,减少更新滞后。

python indexnow.py "https://www.bobobk.com/llms.txt"

常见问题与修复

问题 1:访问 llms.txt 返回 404

原因通常是文件放错位置。

修复:

  1. 确认文件在 static/llms.txt
  2. 重新执行 Hugo 构建
  3. 检查 CDN 缓存并清理

问题 2:llms.txt 文章链接是旧的

原因通常是先生成后构建,或者 RSS 还没更新。

修复:

  1. 先构建站点更新 public/index.xml
  2. 再运行生成脚本
  3. 最后再次构建发布

问题 3:写了太多低质量链接

llms.txt 不是“越长越好”。

建议:

  • 优先放核心分类页与高价值文章
  • 维持结构稳定,避免频繁大改
  • 保持链接可访问且不重定向链过长

总结

如果你现在已经在做内容站,llms.txt 基本属于低成本高收益的动作。

先手写一个最小版本上线,再切换到 Python 自动生成,你就能把“AI 可发现性”从一次性工作升级成稳定流程。配合你已有的 sitemap.xml 和 IndexNow,整体收录与引用效率会更可控。

下一步最实用的做法,就是把生成脚本挂到你每天发文后的构建流程里,让 llms.txt 自动跟着内容更新。

友情链接

其它