对于一个抓取项目,我想在脚本的每次迭代之间添加1秒的延迟。在其他线程中,我已经读过可以通过“时间”功能添加延迟。
但是,下面的代码尽管包含了“时间”,但仍每秒处理多个请求,这对于刮板来说实在太快了。有谁知道如何使1秒延迟正常工作?
import scrapy
import time
custom_settings = {
'ROBOTSTXT_OBEY': False,
'USER_AGENT': "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1"
}
class QuotesSpider(scrapy.Spider):
name = 'spider1'
allowed_domains = ['google.com']
start_urls = ['https://www.google.com/search?q=Activision&biw=1280&bih=607&source=lnt&tbs=cdr%3A1%2Ccd_min%3A01%2F01%2F2004%2Ccd_max%3A12%2F31%2F2004&tbm=nws',
'https://www.google.com/search?q=Activision&biw=1280&bih=607&source=lnt&tbs=cdr%3A1%2Ccd_min%3A01%2F01%2F2005%2Ccd_max%3A12%2F31%2F2005&tbm=nws',
'https://www.google.com/search?q=Activision&biw=1280&bih=607&source=lnt&tbs=cdr%3A1%2Ccd_min%3A01%2F01%2F2006%2Ccd_max%3A12%2F31%2F2006&tbm=nws',
]
def parse(self, response):
item = {
'results': response.css('#resultStats::text')[0].extract(),
'url': response.url,
}
yield item
time.sleep(1)
答案 0 :(得分:1)
对此有一个特殊的设置,称为下载延迟
您可以在文档中阅读更多内容,以获取更多帮助:https://doc.scrapy.org/en/latest/topics/settings.html#download-delay