首页 > 解决方案 > urllib RobotFileParser - robots.txt 中看似冲突的规则

问题描述

以下是 amazon.co.jp/robots.txt 的相关部分:

User-agent: *
Disallow: /-/
Disallow: /gp/aw/shoppingAids/
Allow: /-/en/

我要检查的 URL:"https://www.amazon.co.jp/-/en/035719/dp/B000H4W9WG/ref=sr_1_61?dchild=1&keywords=dot%20matrix%20printer&qid=1617229306&s=computers&sr=1-61"

现在,它符合 disallow:Disallow: /-/也符合 allow: Allow: /-/en/

urllib 的 RobotFileParser 将 URL 标记为 can_fetch = False。我检查了源代码,它似乎是按时间顺序排列的。由于不允许首先出现,它将允许标记为 False,仅此而已。

知道这是否是考虑到 robots.txt 标准的正确方法,因为对我来说这似乎很违反直觉,并且认为应该允许该 url。

相关代码:

import urllib.robotparser
rp = urllib.robotparser.RobotFileParser()
rp.set_url("https://www.amazon.co.jp/robots.txt")
rp.read()
can_ftch = rp.can_fetch("*", "https://www.amazon.co.jp/-/en/035719/dp/B000H4W9WG/ref=sr_1_61?dchild=1&keywords=dot%20matrix%20printer&qid=1617229306&s=computers&sr=1-61")

 

编辑:按照谷歌标准,它应该像我一样工作。该 URL 应该是允许的。the most specific rule based on the length of the [path] entry trumps the less specific (shorter) rule

https://developers.google.com/search/docs/advanced/robots/robots_txt#order-of-precedence-for-group-member-lines

 

EIDT2:做了更多的挖掘,发现了这个 qoute:

对于 Google 和 Bing,规则是具有最多字符的指令获胜。在这里,这是禁止指令。

  • 禁止:/blog/(6 个字符)
  • 允许:/blog(5 个字符)

如果允许和禁止指令的长度相等,则限制最少的指令获胜。在这种情况下,这将是允许指令。

至关重要的是,这仅适用于 Google 和 Bing。其他搜索引擎听第一个匹配指令。在这种情况下,这是不允许的。

按照这个逻辑,RobotFileParser 确实是正确的。

标签: pythonurllibrobots.txt

解决方案


推荐阅读