2016-07-31 93 views
1

我开始2000的URL列表,我使用:添加延迟后500个请求scrapy

DOWNLOAD_DELAY = 0.25 

对于控制要求的速度,但我也想加入正后一个更大的延迟要求。例如,我希望每个请求的延迟时间为0.25秒,每个500个请求的延迟时间为100秒。

编辑:

示例代码:

import os 
from os.path import join 
import scrapy 
import time 

date = time.strftime("%d/%m/%Y").replace('/','_') 

list_of_pages = {'http://www.lapatilla.com/site/':'la_patilla',     
       'http://runrun.es/':'runrunes', 
       'http://www.noticierodigital.com/':'noticiero_digital', 
       'http://www.eluniversal.com/':'el_universal', 
       'http://www.el-nacional.com/':'el_nacional', 
       'http://globovision.com/':'globovision', 
       'http://www.talcualdigital.com/':'talcualdigital', 
       'http://www.maduradas.com/':'maduradas', 
       'http://laiguana.tv/':'laiguana', 
       'http://www.aporrea.org/':'aporrea'} 

root_dir = os.getcwd() 
output_dir = join(root_dir,'data/',date) 

class TestSpider(scrapy.Spider): 
    name = "news_spider" 
    download_delay = 1 

    start_urls = list_of_pages.keys() 

    def parse(self, response): 
     if not os.path.exists(output_dir): 
      os.makedirs(output_dir) 

     filename = list_of_pages[response.url] 
     print time.time() 
     with open(join(output_dir,filename), 'wb') as f: 
      f.write(response.body) 

名单,在这种情况下,较短尚想法是一样的。我希望每个请求都有一个延迟级别,每个'N'请求都有一个延迟级别。 我没有抓取链接,只保存了主页面。

+2

帮助你,这将需要一些更多的代码。 –

+1

请发表[mcve]如果你想要一些好的方法一些帮助,否则这个问题是完全太宽的SO –

+0

@DavidGomes添加代码 –

回答

1

你可以使用AutoThrottle extension来看看,它不会给你一个严格的延迟控制,而是有自己的算法,根据响应时间和并发请求的数量,减慢蜘蛛的速度。

如果您需要更多控制刮取过程的某些阶段的延迟,您可能需要custom middleware或自定义扩展(类似于AutoThrottle - source)。

您也可以在运行中更改.download_delay attribute of your spider。顺便说一下,这正是AutoThrottle扩展功能所做的 - 它是updates the .download_delay value on the fly

一些相关的话题: