AIGC标识 今日头条链接为什么直接请求读不到正文:robots.txt 允许哪个地址、分享链接怎么换成文章地址(附 Python)

本文由 AI 生成:数据来自作者本人开发的链接读取工具 LinkDigest(文档: https://linkdigest.dev/zh/docs )接入今日头条时的记录;文中四段代码都在 2026 年 11 月 24 日重新运行过,输出原样贴出。代码只用 Python 标准库,只请求 robots.txt 允许的地址,请求之间留了间隔。平台的行为会变,请以你自己当下的运行结果为准。

简单说: 用程序(或者联网的 AI)直接请求一条今日头条文章地址,拿回来的是一张 72,914 字节、能看到的文字 0 个的页面,里面也没有承载正文的 RENDER_DATA:正文要等浏览器把页面脚本跑完才会出现。另外,App 分享出来的 /group/<id>/、/item/<id>/ 两种地址,robots.txt 不允许抓取;允许的是 /article/<id>/ 和 /video/<id>/。下面四段代码依次说明:怎么读 robots.txt、普通请求拿回来的是什么、怎么把分享链接换成允许的地址、RENDER_DATA 是什么格式。

一、先读 robots.txt:哪些地址允许

urllib.robotparser 能直接回答「这个地址允许不允许」。这里自己带上 User-Agent 取回 robots.txt,再交给 parse():

import time
import urllib.request
from urllib import robotparser

UA = "robots-demo/1.0 (python urllib; tutorial script)"
ARTICLE_ID = "7709705250554266155"
VIDEO_ID = "7628244045329563689"

for host in ("https://www.toutiao.com", "https://m.toutiao.com"):
    req = urllib.request.Request(host + "/robots.txt", headers={"User-Agent": UA})
    with urllib.request.urlopen(req, timeout=20) as r:
        rp = robotparser.RobotFileParser()
        rp.parse(r.read().decode("utf-8", "replace").splitlines())
    for path in (f"/group/{ARTICLE_ID}/", f"/item/{ARTICLE_ID}/",
                 f"/article/{ARTICLE_ID}/", f"/video/{VIDEO_ID}/"):
        verdict = "允许" if rp.can_fetch("*", host + path) else "不允许"
        print(f"{host:<24} {path:<30} {verdict}")
    time.sleep(2)  # 两次请求之间停一下

输出:

https://www.toutiao.com  /group/7709705250554266155/    不允许
https://www.toutiao.com  /item/7709705250554266155/     不允许
https://www.toutiao.com  /article/7709705250554266155/  允许
https://www.toutiao.com  /video/7628244045329563689/    允许
https://m.toutiao.com    /group/7709705250554266155/    不允许
https://m.toutiao.com    /item/7709705250554266155/     不允许
https://m.toutiao.com    /article/7709705250554266155/  允许
https://m.toutiao.com    /video/7628244045329563689/    允许

www 和 m 两个域名的结论一样:App 分享出来的 /group/ 和 /item/ 都不允许;同一篇文章换成 /article/<id>/ 是允许的,视频在 /video/<id>/,也允许。所以读头条的第一步,是把分享地址里的 id 取出来、换成 /article/ 地址,分享地址本身一次都不请求。

二、普通请求一次允许的地址,拿回来的是什么

请求前先用 robots.txt 确认允许,再请求一次,数一数去掉脚本和样式之后能看到的文字:

import html
import re
import time
import urllib.request
from urllib import robotparser

UA = "robots-demo/1.0 (python urllib; tutorial script)"
URL = "https://www.toutiao.com/article/7709705250554266155/"

req = urllib.request.Request("https://www.toutiao.com/robots.txt", headers={"User-Agent": UA})
with urllib.request.urlopen(req, timeout=20) as r:
    rp = robotparser.RobotFileParser()
    rp.parse(r.read().decode("utf-8", "replace").splitlines())
assert rp.can_fetch("*", URL), "robots.txt 不允许的地址,不请求"
time.sleep(2)

req = urllib.request.Request(URL, headers={"User-Agent": UA})
with urllib.request.urlopen(req, timeout=20) as r:
    status, body = r.status, r.read()

page = body.decode("utf-8", "replace")
no_code = re.sub(r"(?is)<(script|style|noscript)\b.*?</\1\s*>", " ", page)
visible = "".join(html.unescape(re.sub(r"(?s)<[^>]+>", " ", no_code)).split())
print("HTTP", status)
print("字节数:", f"{len(body):,}")
print("能看到的文字:", len(visible), "个字符")
print("有没有 RENDER_DATA:", "RENDER_DATA" in page)

输出:

HTTP 200
字节数: 72,914
能看到的文字: 0 个字符
有没有 RENDER_DATA: False

七万多字节里一个能看到的字都没有,也没有 RENDER_DATA。这是一张要在浏览器里先把脚本跑完、内容才会出来的页面。把头条链接贴给联网的 AI,它说「打不开」或者只能凭标题猜,原因就在这里。

本文不讲怎么绕过这一步:页面的脚本校验、签名、Cookie 都不碰。那样做既不稳定,也违背网站的意愿。

三、把分享链接换成允许的地址(纯函数)

头条文章的 id 是 15 到 20 位的数字。App 分享出来的 /group/<id>/、/item/<id>/,老格式的 /i<id>/、/a<id>/,和 /article/<id>/ 里的是同一个 id。下面这个函数不发请求,只处理字符串:

import re
from urllib.parse import urlsplit

_ARTICLE = re.compile(r"/(?:group|item|article)/(\d{15,20})(?:[/?#]|$)|/[ai](\d{15,20})(?:[/?#]|$)")
_VIDEO = re.compile(r"/video/(\d{15,20})(?:[/?#]|$)")


def allowed_address(pasted: str) -> str | None:
    """粘贴进来的头条链接(可以带着分享文字)→ robots.txt 允许的地址;认不出就返回 None。"""
    m = re.search(r"https?://[^\s,。!]+", pasted)
    if not m:
        return None
    parts = urlsplit(m.group(0))
    host = (parts.hostname or "").lower()
    if host != "toutiao.com" and not host.endswith(".toutiao.com"):
        return None
    video = _VIDEO.search(parts.path)
    if video:
        return f"https://www.toutiao.com/video/{video.group(1)}/"
    article = _ARTICLE.search(parts.path)
    if article:
        return f"https://www.toutiao.com/article/{article.group(1) or article.group(2)}/"
    return None


CASES = [
    "https://m.toutiao.com/group/7709705250554266155/?app=news_article",
    "https://www.toutiao.com/item/7709705250554266155/",
    "https://www.toutiao.com/article/7709705250554266155/?log_from=x",
    "https://www.toutiao.com/i7709705250554266155/",
    "https://www.toutiao.com/a7709705250554266155/",
    "【标题】 https://m.toutiao.com/group/7709705250554266155/ 复制打开",
    "https://www.toutiao.com/video/7628244045329563689/",
    "https://m.toutiao.com/is/AbCd123/",
    "https://www.toutiao.com/w/1834567890123456789/",
]
for case in CASES:
    print(case)
    print("  →", allowed_address(case))

输出:

https://m.toutiao.com/group/7709705250554266155/?app=news_article
  → https://www.toutiao.com/article/7709705250554266155/
https://www.toutiao.com/item/7709705250554266155/
  → https://www.toutiao.com/article/7709705250554266155/
https://www.toutiao.com/article/7709705250554266155/?log_from=x
  → https://www.toutiao.com/article/7709705250554266155/
https://www.toutiao.com/i7709705250554266155/
  → https://www.toutiao.com/article/7709705250554266155/
https://www.toutiao.com/a7709705250554266155/
  → https://www.toutiao.com/article/7709705250554266155/
【标题】 https://m.toutiao.com/group/7709705250554266155/ 复制打开
  → https://www.toutiao.com/article/7709705250554266155/
https://www.toutiao.com/video/7628244045329563689/
  → https://www.toutiao.com/video/7628244045329563689/
https://m.toutiao.com/is/AbCd123/
  → None
https://www.toutiao.com/w/1834567890123456789/
  → None
粘贴进来的 换成的地址
m.toutiao.com/group/<id>/?app=news_article www.toutiao.com/article/<id>/
www.toutiao.com/item/<id>/ www.toutiao.com/article/<id>/
www.toutiao.com/article/<id>/?log_from=x www.toutiao.com/article/<id>/
www.toutiao.com/i<id>/、/a<id>/(老格式) www.toutiao.com/article/<id>/
带着标题和「复制打开」的整段分享文字 www.toutiao.com/article/<id>/
www.toutiao.com/video/<id>/ 原样保留
短链 m.toutiao.com/is/…/ None:要先跟一次跳转才知道 id,不在这个函数里做
微头条 www.toutiao.com/w/<id>/ None:不是文章

几点说明:

  • 函数只认 toutiao.com 和它的子域名,别的网站一律返回 None;
  • 查询参数(分享来源、token 之类)全部丢掉,换成的地址里只有 id;
  • 11 月 20 日用一条新的 /group/ 分享链接实测过:取出 id、换成 /article/<id>/ 之后读到的就是分享的那一篇。

四、RENDER_DATA 是什么格式

在浏览器里把文章页的脚本跑完以后,页面里会出现一段 id="RENDER_DATA" 的 script,内容是 URL 编码过的 JSON,标题、作者、正文都在里面。解码只要两步:urllib.parse.unquote,然后 json.loads。下面用一个编出来的小样本演示(真实页面的内容大得多,字段也多得多):

import json
import re
from urllib.parse import quote, unquote

# 编出来的小样本,只为演示格式:真实页面里这段内容要浏览器把页面脚本跑完才会出现
made_up = {"data": {"title": "示例标题", "mediaInfo": {"name": "示例作者"},
                    "content": "<p>第一段正文。</p><p>第二段正文。</p>"}}
SAMPLE = ('<script id="RENDER_DATA" type="application/json">'
          + quote(json.dumps(made_up, ensure_ascii=False)) + "</script>")
print(SAMPLE[:96] + "…")

raw = re.search(r'<script id="RENDER_DATA"[^>]*>(.*?)</script>', SAMPLE, re.S).group(1)
data = json.loads(unquote(raw))["data"]
paragraphs = [p for p in re.split(r"</p>", data["content"]) if p.strip()]
print("标题:", data["title"])
print("作者:", data["mediaInfo"]["name"])
print("正文:", " / ".join(re.sub(r"<[^>]+>", "", p) for p in paragraphs))

输出:

<script id="RENDER_DATA" type="application/json">%7B%22data%22%3A%20%7B%22title%22%3A%20%22%E7%A…
标题: 示例标题
作者: 示例作者
正文: 第一段正文。 / 第二段正文。

要说清楚的是:真实页面里的这段内容,只有浏览器把页面脚本跑完才会生成,第二节的普通请求拿不到它。如果你要读,请只打开 robots.txt 允许的地址(/article/、/video/),一次一篇,请求之间留出间隔。

小结

  • 先读 robots.txt:/group/、/item/ 不允许,/article/、/video/ 允许,www 和 m 一样;
  • 普通请求文章地址:72,914 字节,能看到的文字 0 个,没有 RENDER_DATA,正文要浏览器跑完脚本才出现;
  • 分享链接里的数字 id 就是文章 id,换成 /article/<id>/ 再读,分享地址本身不请求;
  • RENDER_DATA 是 URL 编码的 JSON:unquote 之后 json.loads。
posted @ 2026-11-24 09:10  LinkDigest  阅读(30)  评论(0)    收藏  举报