设置下载延迟后,Scrapy 行为会发生变化
Scrapy behavior changes when download delay is set
这是我的 scrapy 蜘蛛
class Spider(scrapy.Spider):
name = "name"
start_urls = ["https://www.aurl"]
def parse(self, response):
links_page_urls = response.css("a.dynamic-linkset::attr(href)").extract()
for url in contract_page_urls:
yield response.follow(url, callback=self.parse_list_page)
next_data_cursor = response.css("li.next").css("a::attr(href)").extract_first()
if next_data_cursor:
self.log("going to next page - {}".format(next_data_cursor))
yield response.follow(next_data_cursor, callback=self.parse)
def parse_list_page(self, response):
list = response.css("div.row div.list-group a.list-group-item").css("a::attr(href)").extract()
for url in list:
self.log("url - {}".format(url))
yield scrapy.Request(url=self.base_url + url, callback=self.parse_page)
def parse_page(self, response):
#Lots of code for parsing elements from a page
# build an item and return it
我的观察是,在我自己家里的机器上并且没有设置 download delay
,实际页面被快速连续访问并保存到 mongo。当我将此代码移动到 EC2 实例并将下载延迟设置为 60 时,我现在注意到网页没有被访问以进行抓取,而是访问了第一页,下一个数据令牌被抓取并被访问。然后我看到很多与抓取列表页面相关的打印输出,而不是每个单独的页面。
期望的行为是访问初始 URL,获取页面列表,然后访问每个页面并抓取它,然后移动到下一个数据游标并重复此过程。
您可以尝试设置低得多的 DOWNLOAD_DELAY
(可能 = 2
)并设置 CONCURRENT_REQUESTS = 1
为页面列表中的请求分配更高的优先级,例如:
yield response.follow(url, callback=self.parse_list_page, priority=1)
这是我的 scrapy 蜘蛛
class Spider(scrapy.Spider):
name = "name"
start_urls = ["https://www.aurl"]
def parse(self, response):
links_page_urls = response.css("a.dynamic-linkset::attr(href)").extract()
for url in contract_page_urls:
yield response.follow(url, callback=self.parse_list_page)
next_data_cursor = response.css("li.next").css("a::attr(href)").extract_first()
if next_data_cursor:
self.log("going to next page - {}".format(next_data_cursor))
yield response.follow(next_data_cursor, callback=self.parse)
def parse_list_page(self, response):
list = response.css("div.row div.list-group a.list-group-item").css("a::attr(href)").extract()
for url in list:
self.log("url - {}".format(url))
yield scrapy.Request(url=self.base_url + url, callback=self.parse_page)
def parse_page(self, response):
#Lots of code for parsing elements from a page
# build an item and return it
我的观察是,在我自己家里的机器上并且没有设置 download delay
,实际页面被快速连续访问并保存到 mongo。当我将此代码移动到 EC2 实例并将下载延迟设置为 60 时,我现在注意到网页没有被访问以进行抓取,而是访问了第一页,下一个数据令牌被抓取并被访问。然后我看到很多与抓取列表页面相关的打印输出,而不是每个单独的页面。
期望的行为是访问初始 URL,获取页面列表,然后访问每个页面并抓取它,然后移动到下一个数据游标并重复此过程。
您可以尝试设置低得多的 DOWNLOAD_DELAY
(可能 = 2
)并设置 CONCURRENT_REQUESTS = 1
为页面列表中的请求分配更高的优先级,例如:
yield response.follow(url, callback=self.parse_list_page, priority=1)