(Python爬虫)用urllib.robotparser模块解析某些网站的robots文件时本应返回True的结果却返回了False是怎么回事?
新人第一次发文,哈哈哈哈
问题描述
用以下代码尝试分析简书网站的robots文件。
from urllib.robotparser import RobotFileParser
rp = RobotFileParser('https://www.jianshu.com/robots.txt')
rp.read()
print(rp.can_fetch('*', 'https://www.jianshu.com/mobile/campaign/day_by_day/join?from=home'))
通过观察简书的robots文件推断这里的代码返回结果应该是True,但实际执行时却返回了False。
原因探索
首先要了解can_fetch()方法返回True或False的逻辑是什么样的。在Pycharm中查看can_fetch()方法的部分源码如下:
def can_fetch(self, useragent, url):
"""using the parsed robots.txt decide if useragent can fetch url"""
if self.disallow_all:
return False
if self.allow_all:
return True
可见该方法会首先判断disallow_all属性的值,若为True就将返回False。而这个属性在RobotFileParser类中的默认值是False,也就是说在程序执行过程中它的值被修改为True了。而前面只有一个read()方法可能修改了它的值,所以来看看read()方法的源码:
def read(self):
"""Reads the robots.txt URL and feeds it to the parser."""
try:
f = urllib.request.urlopen(self