gpt4 book ai didi

python - Robotparser 似乎没有正确解析

转载 作者:太空狗 更新时间:2023-10-29 22:19:40 24 4
gpt4 key购买 nike

我正在编写一个爬虫,为此我正在实现 robots.txt 解析器,我正在使用标准库 robotparser

robotparser 似乎没有正确解析,我正在使用 Google 的 robots.txt 调试我的爬虫.

(以下例子来自IPython)

In [1]: import robotparser

In [2]: x = robotparser.RobotFileParser()

In [3]: x.set_url("http://www.google.com/robots.txt")

In [4]: x.read()

In [5]: x.can_fetch("My_Crawler", "/catalogs") # This should return False, since it's on Disallow
Out[5]: False

In [6]: x.can_fetch("My_Crawler", "/catalogs/p?") # This should return True, since it's Allowed
Out[6]: False

In [7]: x.can_fetch("My_Crawler", "http://www.google.com/catalogs/p?")
Out[7]: False

这很有趣,因为它有时似乎“有效”,有时似乎失败了,我也对 Facebook 和 Stackoverflow 的 robots.txt 进行了同样的尝试。这是 robotpaser 模块的错误吗?或者我在这里做错了什么?如果是,是什么?

我想知道 this错误有任何相关

最佳答案

这不是错误,而是解释上的差异。根据draft robots.txt specification (从未获得批准,也不太可能获得批准):

To evaluate if access to a URL is allowed, a robot must attempt to match the paths in Allow and Disallow lines against the URL, in the order they occur in the record. The first match found is used. If no match is found, the default assumption is that the URL is allowed.

(第 3.2.2 节,允许和禁止行)

使用那个解释,然后是“/catalogs/p?”应该被拒绝,因为之前有一个“Disallow:/catalogs”指令。

在某些时候,Google 开始以不同于该规范的方式解释 robots.txt。他们的方法似乎是:

Check for Allow. If it matches, crawl the page.
Check for Disallow. If it matches, don't crawl.
Otherwise, crawl.

问题是对于robots.txt的解释没有正式的协议(protocol)。我见过使用 Google 方法的爬虫和其他使用 1996 年起的标准草案的爬虫。当我操作爬虫时,当我使用 Google 解释时,我从网站管理员那里得到了讨厌的图,因为我爬了他们认为不应该爬的页面,如果我使用其他解释,我会从其他人那里得到 nastygrams,因为他们认为应该编入索引的东西不是。

关于python - Robotparser 似乎没有正确解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15344253/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com