跳转到内容
搜索文档

robots.txt 和 sitemap

最后更新 查看 MarkdownAgent 设置

本页提供关于为计划使用 Browser Run 访问的网站配置 robots.txt 和 sitemap 的一般指导。

识别 Browser Run 请求

可以通过 Cloudflare 附加的自动请求头识别请求:

  • User-Agent:每种 Browser Run 方式有不同的默认 User-Agent,可用于编写针对性的 robots.txt 规则
  • cf-brapi-request-id:Quick Actions 请求的唯一标识符
  • Signature-agent:指向 Cloudflare bot 验证密钥

要使用 WAF 规则而非 robots.txt 允许或阻止 Browser Run 流量,请使用自动请求头页面上的 bot 检测 ID

robots.txt 最佳实践

配置良好的 robots.txt 有助于爬虫了解可以访问网站的哪些部分。

引用 sitemap

robots.txt 中包含 sitemap 引用,以便爬虫发现你的 URL:

robots.txttxt
User-agent: *
Allow: /

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

你可以列出多个 sitemap:

robots.txttxt
User-agent: *
Allow: /

Sitemap: https://example.com/sitemap.xml
Sitemap: https://example.com/blog-sitemap.xml

设置爬取延迟

使用 crawl-delay 控制爬虫请求页面的频率:

robots.txttxt
User-agent: *
Crawl-delay: 2
Allow: /

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

值以秒为单位。crawl-delay 为 2 表示爬虫在请求之间等待两秒。

使用 robots.txt 阻止爬虫

如果你想阻止 Browser Run(或其他爬虫)访问你的网站,可以配置 robots.txt 以限制访问。

阻止所有 bot 访问整个网站

要阻止所有爬虫访问网站上的任何页面:

robots.txttxt
User-agent: *
Disallow: /

这是最严格的配置,会阻止所有合规 bot,而不仅仅是 Browser Run。

仅阻止 /crawl 端点

[/crawl` 端点](/browser-run/quick-actions/crawl-endpoint/) 使用 User-Agent `CloudflareBrowserRenderingCrawler/1.0` 标识自身。要阻止 /crawl` 端点同时允许所有其他流量(包括其他 Browser Run Quick Actions 端点,它们使用不同的 User-Agent):

robots.txttxt
User-agent: CloudflareBrowserRenderingCrawler
Disallow: /

User-agent: *
Allow: /

在特定路径上阻止 /crawl 端点

要允许 ``/crawl` 端点 访问你的网站但阻止特定部分:

robots.txttxt
User-agent: CloudflareBrowserRenderingCrawler
Disallow: /admin/
Disallow: /private/
Allow: /

User-agent: *
Allow: /

sitemap 最佳实践

构建 sitemap 结构以帮助爬虫高效处理你的网站:

sitemap.xmlxml
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <url>
    <loc>https://example.com/important-page</loc>
    <lastmod>2025-01-15T00:00:00+00:00</lastmod>
    <priority>1.0</priority>
  </url>
  <url>
    <loc>https://example.com/other-page</loc>
    <lastmod>2025-01-10T00:00:00+00:00</lastmod>
    <priority>0.5</priority>
  </url>
</urlset>
属性 用途 建议
<loc> 页面 URL 必填。使用完整 URL。
<lastmod> 最后修改日期 包含此字段以帮助爬虫识别更新的内容。使用 ISO 8601 格式。
<priority> 相对重要性(0.0-1.0) 为重要页面设置较高值。爬虫将按优先级顺序处理页面。

Sitemap 索引文件

对于具有多个 sitemap 的大型网站,使用 sitemap 索引文件。Browser Run 使用 depth 参数控制爬取多少层嵌套 sitemap:

sitemap.xmlxml
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  ...
</urlset>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
   <sitemap>
      <loc>https://www.example.com/sitemap-products.xml</loc>
   </sitemap>
   <sitemap>
      <loc>https://www.example.com/sitemap-blog.xml</loc>
   </sitemap>
</sitemapindex>

缓存请求头

Browser Run 定期重新获取 sitemap 以保持内容新鲜。为 sitemap 提供 Last-ModifiedETag 响应头,以便爬虫检测 sitemap 自上次获取以来是否已更改。

建议

  • 在所有 URL 上包含 <lastmod> 以帮助识别哪些页面已更改。使用 ISO 8601 格式(例如 2025-01-15T00:00:00+00:00)。
  • 对于具有多个 sitemap 的大型网站,使用 sitemap 索引文件。
  • 使用 .gz 格式压缩大型 sitemap 以减少带宽。
  • 每个文件保持 sitemap 在 50 MB 和 50,000 URL 以下(标准 sitemap 限制)。

相关资源

这篇文档对您有帮助吗?