使用robotparser模块来解析robots.txt文件,该模块提供了一个RobotFileParser,它可以根据网站的robots.txt文件判断一个爬虫是否有权限爬取这个网页。
语法:
urllib.robotparser.RobotFileParser(url='')
https://www.baidu.com/robots.txt的内容如下(截取部分内容):
User-agent: Baiduspider# 百度爬虫Disallow: /baidu# 不允许爬取/baidu下的内容Disallow: /s?# 不允许爬取/s?下的内容Disallow: /ulink?# 不允许爬取/ulink?下的内容Disallow: /link?# 不允许爬取/link?下的内容Disallow: /home/news/data/# 不允许爬取/home/news/data/下的内容Disallow: /bh# 不允许爬取/bh下的内容User-agent: Googlebot# 谷歌爬虫Disallow: /baidu# 不允许爬取/baidu下的内容Disallow: /s?# 不允许爬取/s?下的内容Disallow: /shifen/# 不允许爬取/shifen/下的内容Disallow: /homepage/# 不允许爬取/homepage/下的内容Disallow: /cpro# 不允许爬取/cpro下的内容Disallow: /ulink?# 不允许爬取/ulink?下的内容Disallow: /link?# 不允许爬取/link?下的内容Disallow: /home/news/data/# 不允许爬取/home/news/data/下的内容Disallow: /bh# 不允许爬取/bh下的内容User-agent: * # 其它爬虫,指robots.txt文件协议中没有指出的其它爬虫Disallow: / # 不允许爬取网站的所有目录及内容
1、方法1:在声明对象时,直接传入robots.txt文件的链接# Python版本:3.6# -*- coding:utf-8 -*-from urllib.robotparser import RobotFileParser# 在声明对象时,直接传入robots.txt文件的链接rp = RobotFileParser('https://www.baidu.com/robots.txt')# 不能缺失,一定要调用此方法,否则can_fetch方法中全都判断为False,该方法用来解析robots.txt文件并进行分析rp.read()# 允许百度爬虫访问 域名的根目录print(rp.can_fetch('Baiduspider','https://www.baidu.com')) # True# 允许百度爬虫访问 目录/homepage/print(rp.can_fetch('Baiduspider','https://www.baidu.com/homepage/')) # True# 禁止谷歌爬虫访问 目录/homepage/print(rp.can_fetch('Googlebot','https://www.baidu.com/homepage/')) # False# 爬虫名称为空或在robots协议中找不到,就匹配"User-agent: *",禁止所有爬虫访问所有目录print(rp.can_fetch('','https://www.baidu.com')) # False
2、方法2:使用set_url()方法注意: 如果在创建RobotFileParser对象时传入了robots.txt文件的链接,就不需要使用方法2。
# Python版本:3.6# -*- coding:utf-8 -*-from urllib.robotparser import RobotFileParserrp = RobotFileParser()# 通过set_url方法设置robots.txt文件的链接rp.set_url('https://www.baidu.com/robots.txt')rp.read()# 允许百度爬虫访问 域名的根目录print(rp.can_fetch('Baiduspider','https://www.baidu.com')) # True# 允许百度爬虫访问 目录/homepage/print(rp.can_fetch('Baiduspider','https://www.baidu.com/homepage/')) # True# 禁止谷歌爬虫访问 目录/homepage/print(rp.can_fetch('Googlebot','https://www.baidu.com/homepage/')) # False# 爬虫名称为空或在robots协议中找不到,就匹配"User-agent: *",禁止所有爬虫访问所有目录print(rp.can_fetch('','https://www.baidu.com')) # False
3、方法3:使用parse方法执行对robots.txt文件的读取和分析# Python版本:3.6# -*- coding:utf-8 -*-from urllib.request import urlopenfrom urllib.robotparser import RobotFileParserrp = RobotFileParser()# 使用parse方法对robots.txt文件的读取和分析rp.parse(urlopen('https://www.baidu.com/robots.txt').read().decode('utf-8').split('n'))# 允许百度爬虫访问 域名的根目录print(rp.can_fetch('Baiduspider', 'https://www.baidu.com')) # True# 允许百度爬虫访问 目录/homepage/print(rp.can_fetch('Baiduspider', 'https://www.baidu.com/homepage/'))# True# 禁止谷歌爬虫访问 目录/homepage/print(rp.can_fetch('Googlebot', 'https://www.baidu.com/homepage/')) # False# 爬虫名称为空或在robots协议中找不到,就匹配"User-agent: *",禁止所有爬虫访问所有目录print(rp.can_fetch('', 'https://www.baidu.com')) # False
4、实际案例# Python版本:3.6# -*- coding:utf-8 -*-import requestsfrom urllib.parse import urlencodefrom urllib.robotparser import RobotFileParserrp = RobotFileParser("https://www.baidu.com/robots.txt")rp.read()dic = {'wd':'爬虫'}url = 'https://www.baidu.com/s?ie=utf-8&' + urlencode(dic)url2 = 'https://www.baidu.com/'headers = { 'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) ' 'Chrome/98.0.4758.80 Safari/537.36'}"""can_fetch方法的第1个参数,填爬虫的名称,可从robots.txt文件中寻找;第2个参数,填要爬取网站的链接"""if rp.can_fetch('Baiduspider',url): print(f'正在爬取网站:{url}') res = requests.get(url=url,headers=headers) print(res.text)elif rp.can_fetch('Baiduspider',url2): print(f'正在爬取网站:{url2}') res = requests.get(url=url2,headers=headers) print(res.text)else: print('网站不允许任何爬虫,爬取网站的内容。')# output:(程序只执行elif分支结构,只有变量url2的链接能爬到内容)