本文共 22484 字,大约阅读时间需要 74 分钟。
ˏ₍•ɞ•₎ˎ有很多粉丝私聊我反馈说:"scrapy框架好难学,自己爬虫基础库已经学差不多了,实战也做了不少,但是好多外包或者老板都要求熟练使用scrapy框架,自己不知道如何下手!"
ˏ₍•ɞ•₎ˎ应粉丝们要求,我苦苦整理➕总结三天三夜,总结出本篇两万字文章,并在文末附带一整套scrapy框架学习路线,如果你能认认真真看完这篇文章,在心里对scrapy有个印象,然后潜心研究文末整套学习路线,那么,scrapy框架对你来说——手到擒来!!!
scrapy设计目的:用于爬取网络数据,提取结构性数据的框架,其中,scrapy使用了Twisted异步网络框架,大大加快了下载速度!
直接pip即可安装scrapy
命令:pip install scrapyscrapy项目开发流程
创建项目:scrapy startproject mySpider生成一个爬虫:scrapy genspider baidu baidu.com提取数据:根据网站结构在spider中实现数据采集相关内容保存数据:使用pipeline进行数据后续处理和保存说明:(scrapy中每个模块的具体作用)
三个内置对象:(scrapy框架中只有三种数据类型)
request请求对象:由url,method,post_data,headers等构成 response响应对象:由url,body,status,headers等构成 item数据对象:本质是一个字典流程原理描述:
1.爬虫中起始的url构造的url对象-->爬虫中间件-->引擎-->调度器 2.调度器把request-->引擎-->下载中间件-->下载器 3.下载器发送请求,获取response响应--->下载中间件--->引擎-->爬虫中间件--->爬虫 4.爬虫提取url地址,组装成request对象--->爬虫中间件--->引擎--->调度器,重复步骤2 5.爬虫提取数据--->引擎--->管道处理和保存数据爬虫中间件和下载中间件只是运行的逻辑的位置不同,作用是重复的:如替换UA等
创建scrapy项目的命令:scrapy startproject <项目名字> 示例:scrapy startproject myspider 项目名字>
生成的目录和文件结果如下:
在项目路径下执行: scrapy genspider <爬虫名字> <允许爬取的域名> cd myspider scrapy genspider itcast itcast.cn 允许爬取的域名> 爬虫名字>
爬虫名字:作为爬虫运行时的参数
允许爬的域名:为对于爬虫设置的爬取范围,设置之后用于过滤要爬取的url,如果爬取的url与允许的域名不同,则被过滤掉命令:在项目目录下执行 scrapy crawl <爬虫名字> 示例:scrapy crawl itcast 爬虫名字>
编写itcast.py爬虫文件:
# -*- coding: utf-8 -*-import scrapyclass ItcastSpider(scrapy.Spider): # 爬虫运行时的参数 name = 'itcast' # 检查允许爬的域名 allowed_domains = ['itcast.cn'] # 1.修改设置起始的url start_urls = ['http://www.itcast.cn/channel/teacher.shtml#ajacaee'] # 数据提取的方法:接收下载中间件传过来的response,定义对于网站相关的操作 def parse(self, response): # 获取所有的教师节点 t_list = response.xpath('//div[@class="li_txt"]') print(t_list) # 遍历教师节点列表 tea_dist = { } for teacher in t_list: # xpath方法返回的是选择器对象列表 extract()方法可以提取到selector对象中data对应的数据。 tea_dist['name'] = teacher.xpath('./h3/text()').extract_first() tea_dist['title'] = teacher.xpath('./h4/text()').extract_first() tea_dist['desc'] = teacher.xpath('./p/text()').extract_first() yield teacher
会发现已经可以OK运行!
# -*- coding: utf-8 -*-import scrapyfrom ..items import UbuntuItemclass ItcastSpider(scrapy.Spider): # 爬虫运行时的参数 name = 'itcast' # 检查允许爬的域名 allowed_domains = ['itcast.cn'] # 1.修改设置起始的url start_urls = ['http://www.itcast.cn/channel/teacher.shtml#ajacaee'] # 数据提取的方法:接收下载中间件传过来的response,定义对于网站相关的操作 def parse(self, response): # 获取所有的教师节点 t_list = response.xpath('//div[@class="li_txt"]') print(t_list) # 遍历教师节点列表 item = UbuntuItem() for teacher in t_list: # xpath方法返回的是选择器对象列表 extract()方法可以提取到selector对象中data对应的数据。 item['name'] = teacher.xpath('./h3/text()').extract_first() item['title'] = teacher.xpath('./h4/text()').extract_first() item['desc'] = teacher.xpath('./p/text()').extract_first() yield item
注意: 1.scrapy.Spider爬虫类中必须有名为parse的解析 2.如果网站结构层次比较复杂,也可以自定义其他解析函数 3.在解析函数中提取的url地址如果要发送请求,则必须属于allowed_domains范围内,但是start_urls中的url地址不受这个限制 4.启动爬虫的时候注意启动的位置,是在项目路径下启动 5.parse()函数中使用yield返回数据,注意:解析函数中的yield能够传递的对象只能是:BaseItem, Request, dict, None 定位元素以及提取数据、属性值的方法:(解析并获取scrapy爬虫中的数据: 利用xpath规则字符串进行定位和提取) 1.response.xpath方法的返回结果是一个类似list的类型,其中包含的是selector对象,操作和列表一样,但是有一些额外的方法 2.额外方法extract():返回一个包含有字符串的列表 3.额外方法extract_first():返回列表中的第一个字符串,列表为空没有返回None response响应对象的常用属性: response.url:当前响应的url地址 response.request.url:当前响应对应的请求的url地址 response.headers:响应头 response.requests.headers:当前响应的请求头 response.body:响应体,也就是html代码,byte类型 response.status:响应状态码
在pipelines.py文件中定义对数据的操作 1.定义一个管道类 2.重写管道类的process_item方法 3.process-item方法处理完item之后必须返回给引擎
升级:
# -*- coding: utf-8 -*-# Define your item pipelines here## Don't forget to add your pipeline to the ITEM_PIPELINES setting# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.htmlimport jsonclass UbuntuPipeline(object): def __init__(self): self.file = open('itcast.json', 'w', encoding='utf-8') def process_item(self, item, spider): # 将item对象强制转为字典,该操作只能在scrapy中使用 item = dict(item) # 爬虫文件中提取数据的方法每yield一次,就会运行一次 # 该方法为固定名称函数 # 默认使用完管道,需要将数据返回给引擎 # 1.将字典数据序列化 '''ensure_ascii=False 将unicode类型转化为str类型,默认为True''' json_data = json.dumps(item, ensure_ascii=False, indent=2) + ',\n' # 2.将数据写入文件 self.file.write(json_data) return item def __del__(self): self.file.close()
在settings文件中,解封代码,说明如下:
(通常在做项目的过程中,在items.py中进行数据建模)
为什么建模? 1.定义item即提前规划好哪些字段需要抓,防止手误,因为定义好之后,在运行过程中,系统会自动检查,值不相同会报错 2.配合注释一起可以清晰的知道要抓取哪些字段,没有定义的字段不能抓取,在目标字段少的时候可以使用字典代替 3.使用scrapy的一些特定组件需要Item做支持,如scrapy的ImagesPipeline管道类
在items.py文件中操作:
# -*- coding: utf-8 -*-# Define here the models for your scraped items## See documentation in:# https://docs.scrapy.org/en/latest/topics/items.htmlimport scrapyclass UbuntuItem(scrapy.Item): # 讲师名字 name = scrapy.Field() # 讲师职称 title = scrapy.Field() # 讲师座右铭 desc = scrapy.Field()
注意: 1.from ..items import UbuntuItem这一行代码中 注意item的正确导入路径,忽略pycharm标记的错误 2.python中的导入路径要诀:从哪里开始运行,就从哪里开始导入
# settings.py文件中找到如下代码解封,并加入UA:# Override the default request headers:DEFAULT_REQUEST_HEADERS = { 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Accept-Language': 'en', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.162 Safari/537.36'}
现在cd到项目目录下,输入scrapy crawl itcast即可运行scrapy!
1.创建项目: scrapy startproject 项目名2.明确目标: 在items.py文件中进行建模3.创建爬虫: 创建爬虫 scrapy genspider 爬虫名 允许的域 完成爬虫 修改start_urls 检查修改allowed_domains 编写解析方法4.保存数据: 在pipelines.py文件中定义对数据处理的管道 在settings.py文件中注册启用管道
requests模块是如何实现翻页请求的: 找到下一页的URL地址 调用requests.get(url)scrapy实现翻页的思路: 找到下一页的url地址 构造url地址的请求对象,传递给引擎 流程: 1.确定url地址 2.构造请求,scrapy.Request(url,callback) callback(回调):指定解析函数名称,表示该请求返回的响应使用哪一个函数进行解析 3.把请求交给引擎(返回):yield scrapy.Request(url,callback)
要求:获取招聘信息职位(职位,技术,地点等)
# -*- coding: utf-8 -*-import scrapyfrom ..items import WangyiItemclass WangyiSpider(scrapy.Spider): name = 'Wangyi' # 修改检查allowed_domains allowed_domains = ['163.com'] start_urls = ['http://hr.163.com/position/list.do'] def parse(self, response): # 提取数据 # 获取所有职位节点列表 node_list = response.xpath('//*[@class="position-tb"]/tbody/tr') # 遍历节点列表 for num, node in enumerate(node_list): # 设置过滤条件,提取数据 if num % 2 == 0: item = WangyiItem() item['name'] = node.xpath('./td[1]/a/text()').extract_first() # item['link'] = 'https://hr.163.com' + node.xpath('./td[1]/a/@href').extract_first() # response.urljoin 用于拼接相对路径的url,可以理解自动补全 item['link'] = response.urljoin(node.xpath('./td[1]/a/@href').extract_first()) item['depart'] = node.xpath('./td[2]/text()').extract_first() item['category'] = node.xpath('./td[3]/text()').extract_first() item['type_job'] = node.xpath('./td[4]/text()').extract_first() item['address'] = node.xpath('./td[5]/text()').extract_first() item['num'] = node.xpath('./td[6]/text()').extract_first().strip() item['date'] = node.xpath('./td[7]/text()').extract_first() yield item # 模拟翻页 # last() 代表只取到最后一页 通过测试可知每次提取的part_url列表中最后一个值即为下一页的url part_url = response.xpath('/html/body/div[2]/div[2]/div[2]/div/a[last()]/@href').extract_first() # 判断是否最后一页 if part_url != 'javascript:void(0)': next_url = response.urljoin(part_url) # 构建请求对象,并且返回给引擎 yield scrapy.Request( url=next_url, # callback 不写默认使用parse方法 callback=self.parse )
# -*- coding: utf-8 -*-# Define your item pipelines here## Don't forget to add your pipeline to the ITEM_PIPELINES setting# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.htmlimport jsonclass WangyiPipeline(object): def __init__(self): self.file = open('wangyi.json', 'w', encoding='utf-8') def process_item(self, item, spider): item = dict(item) str_data = json.dumps(item, ensure_ascii=False, indent=2) + ',\n' self.file.write(str_data) return item
# -*- coding: utf-8 -*-# Define here the models for your scraped items## See documentation in:# https://docs.scrapy.org/en/latest/topics/items.htmlimport scrapyclass WangyiItem(scrapy.Item): # define the fields for your item here like: # name = scrapy.Field() # 职位名称 name = scrapy.Field() # 详情页 link = scrapy.Field() # 部门 depart = scrapy.Field() # 类别 category = scrapy.Field() # 工作类型 type_job = scrapy.Field() # 工作地点 address =scrapy.Field() # 人数 num = scrapy.Field() # 发布时间 date = scrapy.Field()
一关协议:
二设UA: 三开管道:源码:
scrapy.Request(url,[callback,method=“GET”,headers,body,cookies,meta,dont_filter=False])
参数解释 1.中括号里的参数为可选参数 2.callback:表示当前的url的响应交给哪个函数去处理 3.meta:实现数据在不同的解析函数中传递,meta默认带有部分数据,比如下载延迟,请求深度等是个字典参数 4.dont_filter:默认为False,会过滤请求的url地址,即请求过的url地址不会继续被请求,对需要重复请求的url地址可以把它设置为Ture,比如贴吧的翻页请求,页面的数据总是在变化;start_urls中的地址会被反复请求,否则程序不会启动 5.method:指定POST或GET请求 6.headers:接收一个字典,其中不包括cookies 7.cookies:接收一个字典,专门放置cookies 8.body:接收json字符串,为POST的数据,发送payload_post请求时使用
作用:meta可以实现数据在不同的解析函数中的传递 1.meta参数是一个字典 2.meta字典中有一个固定的键proxy,表示代理ip,关于代理ip的使用我们将在看到scrapy的下载中间件时进行介绍
# -*- coding: utf-8 -*-import scrapyfrom ..items import WangyiItemclass WangyiSpider(scrapy.Spider): name = 'Wangyi' # 修改检查allowed_domains allowed_domains = ['163.com'] start_urls = ['http://hr.163.com/position/list.do'] def parse(self, response): # 提取数据 # 获取所有职位节点列表 node_list = response.xpath('//*[@class="position-tb"]/tbody/tr') # 遍历节点列表 for num, node in enumerate(node_list): # 设置过滤条件,提取数据 if num % 2 == 0: item = WangyiItem() item['name'] = node.xpath('./td[1]/a/text()').extract_first() # item['link'] = 'https://hr.163.com' + node.xpath('./td[1]/a/@href').extract_first() # response.urljoin 用于拼接相对路径的url,可以理解自动补全 item['link'] = response.urljoin(node.xpath('./td[1]/a/@href').extract_first()) item['depart'] = node.xpath('./td[2]/text()').extract_first() item['category'] = node.xpath('./td[3]/text()').extract_first() item['type_job'] = node.xpath('./td[4]/text()').extract_first() item['address'] = node.xpath('./td[5]/text()').extract_first() item['num'] = node.xpath('./td[6]/text()').extract_first().strip() item['date'] = node.xpath('./td[7]/text()').extract_first() # 构建详情页的请求 yield scrapy.Request(url=item['link'], callback=self.parse_detail, meta={ 'item':item, # 'proxy':'HTTP://123.149.137.96:9999', }) # 模拟翻页 # last() 代表只取到最后一页 通过测试可知每次提取的part_url列表中最后一个值即为下一页的url part_url = response.xpath('/html/body/div[2]/div[2]/div[2]/div/a[last()]/@href').extract_first() # 判断是否最后一页 if part_url != 'javascript:void(0)': next_url = response.urljoin(part_url) # 构建请求对象,并且返回给引擎 yield scrapy.Request( url=next_url, # callback 不写默认使用parse方法 callback=self.parse ) def parse_detail(self,response): print('--------',response.meta['item']) # 将meta传参获取 item = response.meta['item'] # 提取数据 item['duty'] = response.xpath('/html/body/div[2]/div[2]/div[1]/div/div/div[2]/div[1]/div/text()').extract() item['require'] = response.xpath('/html/body/div[2]/div[2]/div[1]/div/div/div[2]/div[2]/div/text()').extract() print(item) yield item
# -*- coding: utf-8 -*-# Define here the models for your scraped items## See documentation in:# https://docs.scrapy.org/en/latest/topics/items.htmlimport scrapyclass WangyiItem(scrapy.Item): # define the fields for your item here like: # name = scrapy.Field() # 职位名称 name = scrapy.Field() # 详情页 link = scrapy.Field() # 部门 depart = scrapy.Field() # 类别 category = scrapy.Field() # 工作类型 type_job = scrapy.Field() # 工作地点 address =scrapy.Field() # 人数 num = scrapy.Field() # 发布时间 date = scrapy.Field() duty = scrapy.Field() require = scrapy.Field()
requests模块是如何实现模拟登陆的? 1.直接携带cookies请求页面 2.找url地址,发送post请求存储cookiescrapy的模拟登陆: 1.直接携带cookies 2.找url地址,发送post请求存储cookiescrapy携带cookies直接获取需要登陆后的页面应用场景: 1.cookie过期时间很长,常见于一些不规范的网站 2.能在cookie过期之前把所有的数据拿到 3.配合其他程序使用,比如其使用selenium把登陆之后的cookie获取到保存到本地,scrapy发送请求之前先读取本地cookie通过获取标题获取是否登录成功状态!
注意 1.cookies从request headers中获取,在配置文件settings中,添加user-agent,注释掉robot协议 2.对应的,如果start_url地址中的url是需要登录后才能访问的url地址,则需要重写start_request方法并在其中手动添加上cookie 3.scrapy中cookie不能够放在headers中,在构造请求的时候有专门的cookies参数,能够接受字典形式的coookie
通过scrapy.Request()指定method、body参数来发送post请求;但是通常使用scrapy.FormRequest()来发送post请求!发送post请求: 注意:scrapy.FormRequest()能够发送表单和ajax请求,参考阅读 https://www.jb51.net/article/146769.htm思路分析: 1.找到post的url地址:点击登录按钮进行抓包,然后定位url地址为https://github.com/session 2.找到请求体的规律:分析post请求的请求体,其中包含的参数均在前一次的响应中 3.是否登录成功:通过请求个人主页,观察是否包含用户名
登录github为例:
# -*- coding: utf-8 -*-import scrapyfrom ..items import UbuntuItemclass ItcastSpider(scrapy.Spider): # 爬虫运行时的参数 name = 'itcast' # 检查允许爬的域名 allowed_domains = ['github.com'] # 1.修改设置起始的url start_urls = ['https://github.com/login'] def parse(self, response): # 从登陆页面响应中解析post数据 token = response.xpath('//*[@id="login"]/form/input[1]/@value').extract_first() post_data = { 'commit': 'Sign in', 'authenticity_token': token, 'login': '1915344876@qq.com', 'password': '在这输自己的密码', 'webauthn - support': 'supported', } print(post_data) # 针对登陆url发送post请求 yield scrapy.FormRequest( url='https://github.com/session', callback=self.after_login, formdata=post_data ) def after_login(self, response): yield scrapy.Request('https://github.com/1915344876/first', # 此处的url是登录之后才可访问的url,以校验是否登录成功! callback=self.check_login) def check_login(self, response): print(response.xpath('/html/head/title/text()').extract_first())
pipeline中常用的方法: 1.process_item(self,item,spider): 管道类中必须有的函数 实现对item数据的处理 必须return item 2.open_spider(self, spider): 在爬虫开启的时候仅执行一次 3.close_spider(self, spider): 在爬虫关闭的时候仅执行一次
思考:在settings中能够开启多个管道,为什么需要开启多个? 1.不同的pipeline可以处理不同爬虫的数据,通过spider.name属性来区分 2.不同的pipeline能够对一个或多个爬虫进行不同的数据处理的操作,比如一个进行数据清洗,一个进行数据的保存 3.同一个管道类也可以处理不同爬虫的数据,通过spider.name属性来区分pipeline使用注意点: 1.使用之前需要在settings中开启 2.pipeline在setting中键表示位置(即pipeline在项目中的位置可以自定义),值表示距离引擎的远近,越近数据会越先经过:权重值小的优先执行 3.有多个pipeline的时候,process_item的方法必须return item,否则后一个pipeline取到的数据为None值 4.pipeline中process_item的方法必须有,否则item没有办法接受和处理 5.process_item方法接受item和spider,其中spider表示当前传递item过来的spider 6.open_spider(spider) :能够在爬虫开启的时候执行一次 7.close_spider(spider) :能够在爬虫关闭的时候执行一次 8.上述俩个方法经常用于爬虫和数据库的交互,在爬虫开启的时候建立和数据库的连接,在爬虫关闭的时候断开和数据库的连接
crawlspider是什么?
回顾之前的代码中,我们有很大一部分时间在寻找下一页的url地址或者是内容的url地址上面,这个过程能更简单一些么?
思路:
1.从response中提取所有的满足规则的url地址 2.自动的构造自己requests请求,发送给引擎对应的crawlspider就可以实现上述需求,能够匹配满足条件的url地址,组装成Reuqest对象后自动发送给引擎,同时能够指定callback函数 即:crawlspider爬虫可以按照规则自动获取链接。
scrapy genspider -t crawl job 163.com
生成crawlspider爬虫文件
使用crawlspider编写实战项目网易招聘爬虫:# -*- coding: utf-8 -*-import scrapyfrom scrapy.linkextractors import LinkExtractorfrom scrapy.spiders import CrawlSpider, Ruleimport timeclass JobCrawlSpider(CrawlSpider): name = 'job_crawl' allowed_domains = ['163.com'] start_urls = ['http://hr.163.com/position/list.do'] # 存放链接处理规则,可为元组与列表 rules = ( # Rule 链接处理规则对象 # LinkExtractor用于定义提取规则 # callback 设置链接提取规则提取的链接对应的响应由谁处理 # follow 决定是否继续在链接提取器提取的链接对应的响应中继续应用链接提取器,默认是False Rule(LinkExtractor(allow=r'\?currentPage=\d+$'), callback='parse_item', follow=True), ) # 没有parse方法了哦! def parse_item(self, response): # 获取所有职位节点列表 node_list = response.xpath('//*[@class="position-tb"]/tbody/tr') print(node_list) # 遍历节点列表 for num,node in enumerate(node_list): if num % 2 == 0: item = { } item['name'] = node.xpath('./td[1]/a/text()').extract_first() # item['link'] = 'https://hr.163.com' + node.xpath('./td[1]/a/@href').extract_first() # response.urljoin 用于拼接相对路径的url,可以理解自动补全 item['link'] = response.urljoin(node.xpath('./td[1]/a/@href').extract_first()) item['depart'] = node.xpath('./td[2]/text()').extract_first() item['category'] = node.xpath('./td[3]/text()').extract_first() item['type_job'] = node.xpath('./td[4]/text()').extract_first() item['address'] = node.xpath('./td[5]/text()').extract_first() item['num'] = node.xpath('./td[6]/text()').extract_first().strip() item['date'] = node.xpath('./td[7]/text()').extract_first() time.sleep(2) yield item
跟普通的scrapy.spider的区别: 在crawlspider爬虫中,没有parse函数重点在rules中: 1.rules是一个元组或者是列表,包含的是Rule对象 2.Rule表示规则,其中包含LinkExtractor,callback和follow等参数 3.LinkExtractor:连接提取器,可以通过正则或者是xpath来进行url地址的匹配 4.callback :表示经过连接提取器提取出来的url地址响应的回调函数,可以没有,没有表示响应不会进行回调函数的处理 5.follow:连接提取器提取的url地址对应的响应是否还会继续被rules中的规则进行提取,True表示会,Flase表示不会
1.除了用命令scrapy genspider -t crawl <爬虫名>创建一个crawlspider的模板,也可以手动创建2.crawlspider中不能再有以parse为名的数据提取方法,该方法被crawlspider用来实现基础url提取等功能3.Rule对象中LinkExtractor为固定参数,其他callback、follow为可选参数4.不指定callback且follow为True的情况下,满足rules中规则的url还会被继续提取和请求5.如果一个被提取的url满足多个Rule,那么会从rules中选择一个满足匹配条件的Rule执行 爬虫名>
链接提取器LinkExtractor的更多常见参数: allow: 满足括号中的're'表达式的url会被提取,如果为空,则全部匹配 deny: 满足括号中的're'表达式的url不会被提取,优先级高于allow allow_domains: 会被提取的链接的domains(url范围),如: ['hr.tencent.com', 'baidu.com'] deny_domains: 不会被提取的链接的domains(url范围) restrict_xpaths: 使用xpath规则进行匹配,和allow共同过滤url,即xpath满足的范围内的url地址会被提取,如: restrict_xpaths='//div[@class="pagenav"]' Rule另一个参数: process_links: 当链接提取器LinkExtractor获取到链接列表的时候调用该参数指定的方法,这个自定义方法可以用来过滤url,且这个方法执行后才会执行callback指定的方法
scrapy中间件的分类,根据scrapy运行流程中所在位置不同分为: 1.下载中间件 2.爬虫中间件scrapy中间件的作用:预处理request和response对象 1.对header以及cookie进行更换和处理 2.使用代理ip等 3.对请求进行定制化操作但在scrapy默认的情况下 两种中间件都在middlewares.py一个文件中爬虫中间件使用方法和下载中间件相同,且功能重复,通常使用下载中间件
接下来我们对项目实战招聘爬虫进行修改完善,通过下载中间件来实现如何使用中间件编写一个Downloader Middlewares。
流程: 和我们编写一个pipeline一样,定义一个类,然后在setting中开启。
Downloader Middlewares默认的方法:(在中间件类中,重写处理请求或者响应的方法)process_request(self, request, spider): 当每个request通过下载中间件时,该方法被调用。 1.返回None值:没有return也是返回None,该request对象传递给下载器,或通过引擎传递给其他权重低的process_request方法 即:如果所有的下载器中间件都返回为None,则请求最终被交给下载器处理 2.返回Response对象:不再请求,把response返回给引擎 即:将响应对象交给 spider 进行解析 3.返回Request对象:把request对象通过引擎交给调度器,此时将不通过其他权重低的process_request方法 即:如果返回为请求,则将请求交给调度器 process_response(self, request, response, spider): 当下载器完成http请求,传递响应给引擎的时候调用 1.返回Resposne:通过引擎交给爬虫处理或交给权重更低的其他下载中间件的process_response方法 即:将响应对象交给 spider 进行解析 2.返回Request对象:通过引擎交给调取器继续请求,此时将不通过其他权重低的process_request方法 即:如果返回为请求,则将请求交给调度器 此步骤不要忘记:在settings.py中配置开启中间件,权重值越小越优先执行
在middlewares文件中,完善代码:
在settings中设置开启自定义的下载中间件,设置方法同管道:思路分析: 1.代理添加的位置:request.meta中增加proxy字段 2.获取一个代理ip,赋值给request.meta['proxy'] 代理池中随机选择代理ip 代理ip的webapi发送请求获取一个代理ip别忘记在settings文件中注册: 检测代理ip是否可用(也可以通过断言的方法 assert 验证过滤IP):
如果页面中有ajax 渲染延迟,则需要使用selenium。
以github模拟登录为例: 第一步:编写爬虫文件!import scrapyclass Login4Spider(scrapy.Spider): name = 'login4' allowed_domains = ['github.com'] start_urls = ['https://github.com/1915344876/first'] # 直接对验证的url发送请求 def parse(self, response): with open('check.html', 'w') as f: f.write(response.body.decode())
第二步:在middlewares.py中使用selenium!
# -*- coding: utf-8 -*-# Define here the models for your spider middleware## See documentation in:# https://docs.scrapy.org/en/latest/topics/spider-middleware.htmlimport timefrom selenium import webdriverdef getCookies(): # 使用selenium模拟登陆,获取并返回cookie username = input('输入github账号:') password = input('输入github密码:') options = webdriver.ChromeOptions() options.add_argument('--headless') options.add_argument('--disable-gpu') driver = webdriver.Chrome(executable_path="C:\my\Chrome_guge\chromedriver.exe", chrome_options=options) driver.get('https://github.com/login') time.sleep(1) driver.find_element_by_xpath('//*[@id="login_field"]').send_keys(username) time.sleep(1) driver.find_element_by_xpath('//*[@id="password"]').send_keys(password) time.sleep(1) driver.find_element_by_xpath('//*[@id="login"]/form/div[3]/input[3]').click() time.sleep(2) cookies_dict = { cookie['name']: cookie['value'] for cookie in driver.get_cookies()} driver.quit() return cookies_dictclass LoginDownloaderMiddleware(object): def process_request(self, request, spider): cookies_dict = getCookies() print(cookies_dict) request.cookies = cookies_dict # 对请求对象的cookies属性进行替换
通过上面的学习,你已经可以独立创建一个scrapy项目并使用此框架进行简单的爬虫项目编写。但是!任何一种功夫都不是一下就能学好学会学精的!所以下面分享一波scrapy学习路线,只要你跟着潜心学完,那么!恭喜你!你已经是名优秀的scrapy框架使用者了!!!
转载地址:http://ezlzi.baihongyu.com/