【发布时间】:2021-07-26 02:00:31
【问题描述】:
我知道我可以自己检查是否存在 robots.txt 文件,使用 python 并触发 http(s) 请求。由于 Scrapy 正在检查和下载它以便让蜘蛛遵守其中的规则,蜘蛛类中是否有属性或方法或任何东西可以让我知道是否存在 robots.txt 以供抓取给定的网站?
尝试使用爬虫统计信息:
见here
self.crawler.stats.inc_value(f'robotstxt/response_status_count/{response.status}')
我对有和没有 robots.txt 的网站进行了几次测试,我可以看到有关 robots.txt 存在的正确信息。例如,在我看到的 spider_close 信号处理程序的 Spider 类中记录 self.crawler.stats.__dict__:
'robotstxt/response_status_count/200': 1 带有 robots.txt 的网站
'robotstxt/response_status_count/404': 1 没有robots.txt的网站
如果蜘蛛在抓取过程中遇到多个域,这将不起作用,并且统计结果将类似于:
"robotstxt/response_status_count/200": 1,
"robotstxt/response_status_count/301": 6,
"robotstxt/response_status_count/404": 9,
"robotstxt/response_status_count/403": 1
但我无法将 HTTP 状态代码响应映射到域...
【问题讨论】:
标签: python scrapy robots.txt