AI 爬虫到底从你的网站得到什么?一个 66 行的脚本

发布于

简短回答

分别以浏览器和各个 AI 爬虫的身份请求你的页面,然后对比结果。如果只有爬虫收到 403、429 或验证页面,或者返回 200 但内容比浏览器看到的小得多,就说明防火墙拒绝了它,即使 robots.txt 允许。这个 66 行的 Node 脚本会测试七个爬虫,并按 RFC 9309 规则读取 robots.txt。

robots.txt 只告诉爬虫可以抓取什么,却不会说明防火墙或 CDN 实际返回了什么。本文提供一个简短的脚本,把两者并排显示,让你看出它们在哪里不一致。

本页内容
  1. 为什么 robots.txt 不能告诉你爬虫得到了什么?
  2. 脚本
  3. 如何运行?
  4. 输出是什么样的?
  5. 如何阅读这张表?
  6. 脚本做不到什么?
  7. 真实网站上我们看到了什么?

为什么 robots.txt 不能告诉你爬虫得到了什么?

robots.txt 只是一个请求。真正返回什么内容,由防火墙、CDN 或机器人防护规则决定。它们可能对 OAI-SearchBot 或 PerplexityBot 返回 403、429 或“Just a moment...”页面,而你用自己的浏览器访问时一切正常。

检查的方法是以浏览器和爬虫两种身份请求同一个 URL,然后对比。只有爬虫收到 403、429 或 503,页面标题为 Just a moment...,或者返回 200 但内容比浏览器的小得多,这些都指向防火墙。

脚本

将它保存为 ai-crawler-view.mjs。它需要 Node 18 或更高版本,不需要任何依赖包。它先读取一次 robots.txt,然后以浏览器身份获取你的页面,再依次以七个 AI 爬虫的身份获取:OAI-SearchBot、ChatGPT-User、GPTBot、Claude-SearchBot、ClaudeBot、PerplexityBot 和 Perplexity-User。

#!/usr/bin/env node
// ai-crawler-view: what do AI crawlers get from YOUR site, compared with a browser?
// Usage: node ai-crawler-view.mjs https://your-site.com/page [--expect "a sentence from your page"]
// Node 18+, no dependencies. Run it against sites you own: it sends requests with crawler user agents.
const BROWSER = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36";
const BOTS = [ // [robots.txt token, user agent]; Anthropic prints no full strings, so Claude gets a token-based one
  ["OAI-SearchBot", "Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko); compatible; OAI-SearchBot/1.0; +https://openai.com/searchbot"],
  ["ChatGPT-User", "Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko); compatible; ChatGPT-User/1.0; +https://openai.com/bot"],
  ["GPTBot", "Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko); compatible; GPTBot/1.1; +https://openai.com/gptbot"],
  ["Claude-SearchBot", "Mozilla/5.0 (compatible; Claude-SearchBot/1.0)"],
  ["ClaudeBot", "Mozilla/5.0 (compatible; ClaudeBot/1.0)"],
  ["PerplexityBot", "Mozilla/5.0 (compatible; PerplexityBot/1.0; +https://perplexity.ai/perplexitybot)"],
  ["Perplexity-User", "Mozilla/5.0 (compatible; Perplexity-User/1.0; +https://perplexity.ai/perplexity-user)"],
];
const BLOCK = new Set([401, 403, 406, 429, 451, 503, 999]);
const CHALLENGE = /Just a moment\.\.\.|Attention Required! \| Cloudflare|\/cdn-cgi\/challenge-platform|_Incapsula_Resource|Access Denied/i;

const [url, ...rest] = process.argv.slice(2);
const expect = rest.includes("--expect") ? rest[rest.indexOf("--expect") + 1] : "";
if (!url) { console.error("usage: node ai-crawler-view.mjs https://site/page [--expect \"text\"]"); process.exit(2); }

async function get(u, ua) {
  try {
    const r = await fetch(u, { headers: { "user-agent": ua }, redirect: "follow", signal: AbortSignal.timeout(15000) });
    return { status: r.status, body: (await r.text()).slice(0, 500000) };
  } catch (e) { return { status: 0, body: "", error: e.cause?.code || e.name }; }
}

// robots.txt, RFC 9309 in short: the group naming the token wins, else "*"; longest match wins; Allow wins a tie.
function robotsVerdict(txt, token, path) {
  const groups = []; let cur = null;
  for (const raw of txt.split(/\r?\n/)) {
    const m = raw.replace(/#.*/, "").match(/^\s*([A-Za-z-]+)\s*:\s*(.*?)\s*$/); if (!m) continue;
    const [k, v] = [m[1].toLowerCase(), m[2]];
    if (k === "user-agent") { if (!cur || cur.rules.length) groups.push(cur = { agents: [], rules: [] }); cur.agents.push(v.toLowerCase()); }
    else if (cur && (k === "allow" || k === "disallow") && v) cur.rules.push([k, v]);
  }
  const g = groups.find(x => x.agents.includes(token.toLowerCase())) || groups.find(x => x.agents.includes("*"));
  if (!g) return "no rule";
  let best = null;
  for (const [k, p] of g.rules) {
    const re = new RegExp("^" + p.replace(/[.+?^${}()|[\]\\]/g, "\\$&").replace(/\*/g, ".*").replace(/\\\$$/, "$"));
    if (re.test(path) && (!best || p.length > best[1].length || (p.length === best[1].length && k === "allow"))) best = [k, p];
  }
  return best ? `${best[0]}: ${best[1]}` : "no rule";
}

const target = new URL(url);
const robots = await get(new URL("/robots.txt", target).href, BROWSER);
const robotsOk = robots.status >= 200 && robots.status < 300 && !/<html/i.test(robots.body);
const control = await get(url, BROWSER);
const cLen = control.body.length;
console.log(`control (browser): HTTP ${control.status}, ${cLen} bytes${expect ? `, expected text ${control.body.includes(expect) ? "found" : "MISSING"}` : ""}`);
if (control.status === 0 || BLOCK.has(control.status)) console.log("  the browser request itself failed or was refused: results below are not meaningful");
console.log(`robots.txt: ${robots.status >= 500 ? "server error (RFC 9309: crawlers must assume everything is disallowed)" : robotsOk ? "read" : "none or not valid"}\n`);
console.log("crawler           robots.txt          HTTP  bytes   verdict");
for (const [token, ua] of BOTS) {
  const r = await get(url, ua);
  const rule = robotsOk ? robotsVerdict(robots.body, token, target.pathname + target.search) : "no rule";
  const challenge = CHALLENGE.test(r.body.slice(0, 20000)) && r.body.length < 20000;
  const verdict = BLOCK.has(r.status) || challenge ? "BLOCKED by the server"
    : r.status >= 400 || r.status === 0 ? `error ${r.status || r.error}`
    : r.body.length < cLen * 0.5 ? "THIN: much smaller than the browser's page"
    : expect && !r.body.includes(expect) ? "text MISSING from the HTML" : "ok";
  console.log(`${token.padEnd(17)} ${rule.padEnd(19)} ${String(r.status).padEnd(5)} ${String(r.body.length).padEnd(7)} ${verdict}${rule.startsWith("disallow") ? "  (robots.txt asks it to stay away)" : ""}`);
}

Anthropic 没有公布完整的 user agent 字符串,所以两个 Claude 的请求只使用爬虫标识。按标识匹配的防火墙会做出同样的反应。

如何运行?

请对你自己网站的页面运行它。可选的 --expect 参数会检查,主要内容中的某句话是否出现在每个爬虫收到的原始 HTML 里。如果没有,说明文本是脚本在页面加载后才添加的,很多爬虫看不到。

node ai-crawler-view.mjs https://your-site.com/pricing/ --expect "a sentence from your page"

只在你自己的或获准测试的网站上运行。它会发送九个请求:robots.txt、浏览器,以及每个爬虫各一个。

输出是什么样的?

下面是对一个特意设成“敌对”的本地测试服务器运行的结果:它对 OAI-SearchBot 返回验证页面,对 ClaudeBot 限流,对 PerplexityBot 返回空壳页面,并在 robots.txt 中禁止 GPTBot:

control (browser): HTTP 200, 2618 bytes, expected text found
robots.txt: read

crawler           robots.txt          HTTP  bytes   verdict
OAI-SearchBot     allow: /            403   31      BLOCKED by the server
ChatGPT-User      allow: /            200   2618    ok
GPTBot            disallow: /         200   2618    ok  (robots.txt asks it to stay away)
Claude-SearchBot  allow: /            200   2618    ok
ClaudeBot         allow: /            429   9       BLOCKED by the server
PerplexityBot     allow: /            200   36      THIN: much smaller than the browser's page
Perplexity-User   allow: /            200   2618    ok

在 aiseewhy.com 上,每个爬虫都得到完整页面,状态码和大小与浏览器相同。

如何阅读这张表?

脚本做不到什么?

真实网站上我们看到了什么?

AISeeWhy 会对 14 个爬虫做这类检查。在我们能够测试的美国、英国和法国的 165 个企业网站中,38%(63 个网站)在服务器层面拒绝了至少一个 AI 爬虫,而来自同一地址的浏览器却能得到页面。这些网站无一例外至少拒绝了一个训练或抓取类机器人,另有 10%(16 个网站)还至少拒绝了一个 AI 搜索爬虫,例如 PerplexityBot(11 个网站)或 OAI-SearchBot(5 个)。

这些网站不是随机样本,另有 53 个网站无法测试,因为连普通的浏览器请求也被拒绝或没有响应。请把它当作值得在你自己网站上检查的一种模式,而不是全面普查。

免费工具

下一步

本文涉及的爬虫

常见问题

对任何网站运行它都安全吗?

只在你自己的或获准测试的网站上运行。它会用爬虫的 user agent 发送请求,有些机器人防御会把这种请求视为恶意行为。

robots.txt 禁止了 GPTBot,为什么还显示 ok?

robots.txt 只是请求,不是锁。服务器会回应任何无视它的请求,所以 HTTP 一列显示 ok。旁边的规则显示的是遵守规范的爬虫会服从的内容。

脚本会运行 JavaScript 吗?

不会,这是有意为之。它展示的是不渲染页面的爬虫收到的 HTML。--expect 参数会告诉你,主要文本是否已经包含在这份 HTML 中。

结果正常是否意味着 ChatGPT 会引用我的页面?

不是。能访问是必要条件,但不是充分条件。想知道某个助手是否会提到你的网站,可以向它提出你的客户会问的问题,并记下它引用了哪些网站。

参考资料

更多文章