基于优先获取item的想法,最下级请求最优先
请求优先级是基于scrapy有很多请求要发起的情况
priority
越大请求越优先
不在设置中修改配置
scrapy代码太复杂,这是目前可以接受的解决办法
class xxxspiderSpider(scrapy.Spider):
# 三级请求优先级逐级递减
priority1 = 100000000
priority2 = priority1 * 10000
priority3 = priority2 * 10000
def start_requests(self):
yield scrapy.http.Request(第一级url,callback=self.第一级爬取,priority=self.priority1_func())
def 第一级爬取(self, response):
for i in 第二级url列表:
yield scrapy.http.Request(url=第二级url,callback=self.第二级爬取,priority=self.priority2_func())
yield scrapy.http.Request(第一级下一页url,callback=self.第一级爬取,priority=self.priority1_func())
def 第二级爬取(self, response):
for i in 第三级url列表:
yield scrapy.http.Request(第三级url,callback=self.第三级爬取,priority=self.priority3_func())
yield scrapy.http.Request(第二级下一页url,callback=self.第二级爬取,priority=self.priority2_func())
def 第三级爬取(self, response):
yield item
def priority1_func(self):
self.priority1 = self.priority1 -1
return self.priority1
def priority2_func(self):
self.priority2 = self.priority2 -1
return self.priority2
def priority3_func(self):
self.priority3 = self.priority3 -1
return self.priority3
标签:priority1,self,多级,priority,scrapy,priority2,func
From: https://www.cnblogs.com/meizhengchao/p/16591718.html