如何在 Scrapy 框架中使用 RobotsTxtMiddleware?

Max*_*Max 6 python robots.txt scrapy

Scrapy 框架有 RobotsTxtMiddleware。它需要确保 Scrapy 尊重 robots.txt。它需要在设置ROBOTSTXT_OBEY = True中设置,然后 Scrapy 会尊重 robots.txt 策略。我做到了并运行了蜘蛛。在调试中,我看到了对http://site_url/robot.txt 的请求。

  1. 这是什么意思,它是如何工作的?
  2. 我如何处理响应?
  3. 如何查看和理解robot.txt 中的规则?

ami*_*che 4

robot.txt间谍程序要求规则所在,这是正常的。

robot.txt基本上是一个您不应该访问/抓取的网址黑名单,它使用 glob/regex 类型的语法来指定禁止的网址。

Scapy 将读取robot.txt这些规则并将其翻译为代码。在抓取过程中,当间谍程序遇到某个 URL 时,它首先会根据从 URL 生成的规则来验证robot.txt该 URL 是否可以访问。如果 URL 没有被robot.txtscrapy 列入黑名单,则会访问该 url 并传递一个Response.

robot.txt不仅将网址列入黑名单,还提供抓取发生的速度。这是一个例子robot.txt

User-Agent: * 
Disallow: /x?
Disallow: /vote?
Disallow: /reply?
Disallow: /submitted?
Disallow: /submitlink?
Disallow: /threads?
Crawl-delay: 30
Run Code Online (Sandbox Code Playgroud)