Scrapy DOWNLOAD_DELAY 不适用于顺序请求
Scrapy DOWNLOAD_DELAY not working for sequential requets
我目前正在使用 Scrapy Python 库。
首先,我对 Fitbit 的登录页面 (https://www.fitbit.com/login) to log myself in. Then I make close to 100 requests to the Fitbit's API (https://api.fitbit.com) 进行了 FormRequest 调用。
为了不强调 API(并且不被禁止!),我想在 settings.py 文件中使用 DOWNLOAD_DELAY 设置请求之间的延迟。但是它不起作用。
我在教程 (http://scrapy.readthedocs.io/en/latest/intro/tutorial.html) 中对其进行了测试,它在那里工作正常。
你怎么看?是因为我请求 API(应该处理这些类型的访问)吗?
编辑:这是我的蜘蛛的伪代码:
class FitbitSpider:
start_urls = ["https://www.fitbit.com/login"]
def parse(self, response):
yield scrapy.FormRequest(url,formdata,callback=after_login)
def after_login(self, response):
for i in range(100):
yield scrapy.Request("https://api.fitbit.com/[...]")
编辑 2:这是我的 settings.py 文件:
BOT_NAME = 'fitbitscraper'
SPIDER_MODULES = ['fitbitscraper.spiders']
NEWSPIDER_MODULE = 'fitbitscraper.spiders'
DOWNLOAD_DELAY = 20 #20 seconds of delay should be pretty noticeable
DOWNLOAD_DELAY: The amount of time (in secs) that the downloader should wait before downloading consecutive pages from the same
website. This can be used to throttle the crawling speed to avoid
hitting servers too hard.
正如我们在那里看到的那样,此配置仅影响来自同一网站的 连续页面 ,这是因为已分配 slots
的抓取工具。默认情况下,scrapy
为每个域设置一个插槽(因为这个想法是每个插槽都应该处理自己的速度)。
现在,您还可以更改可以使用 meta
变量 download_slot
处理的 slot
请求,因此如果您不这样做,请确保您没有使用该变量不知道它能做什么。
其他设置也会干扰 DOWNLOAD_DELAY
,例如:
因此请确保它们未启用,或者您没有尝试在同一个项目中使用这两种设置。
另外需要指出的是,download_delay
也可以作为 Spider
变量启用,并且它优先于设置中的变量。
好的,我刚刚找到问题的答案。
它来自于我 运行 在 main.py 文件中创建 CrawlerProcess。它没有加载 settings.py 文件中的设置。
之前我做了以下事情:
process = CrawlerProcess({
'USER_AGENT': 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)'
})
process.crawl(fitbit_spider.FitbitSpider)
process.start()
现在,如果我将 CrawlerProcess 更改为:
process = CrawlerProcess({
'USER_AGENT': 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)',
'DOWNLOAD_DELAY': 20
})
我确实得到了想要的延迟!
注意:使用 get_project_settings()
创建 CrawlerProcess 也不起作用。
我目前正在使用 Scrapy Python 库。
首先,我对 Fitbit 的登录页面 (https://www.fitbit.com/login) to log myself in. Then I make close to 100 requests to the Fitbit's API (https://api.fitbit.com) 进行了 FormRequest 调用。
为了不强调 API(并且不被禁止!),我想在 settings.py 文件中使用 DOWNLOAD_DELAY 设置请求之间的延迟。但是它不起作用。
我在教程 (http://scrapy.readthedocs.io/en/latest/intro/tutorial.html) 中对其进行了测试,它在那里工作正常。
你怎么看?是因为我请求 API(应该处理这些类型的访问)吗?
编辑:这是我的蜘蛛的伪代码:
class FitbitSpider:
start_urls = ["https://www.fitbit.com/login"]
def parse(self, response):
yield scrapy.FormRequest(url,formdata,callback=after_login)
def after_login(self, response):
for i in range(100):
yield scrapy.Request("https://api.fitbit.com/[...]")
编辑 2:这是我的 settings.py 文件:
BOT_NAME = 'fitbitscraper'
SPIDER_MODULES = ['fitbitscraper.spiders']
NEWSPIDER_MODULE = 'fitbitscraper.spiders'
DOWNLOAD_DELAY = 20 #20 seconds of delay should be pretty noticeable
DOWNLOAD_DELAY: The amount of time (in secs) that the downloader should wait before downloading consecutive pages from the same website. This can be used to throttle the crawling speed to avoid hitting servers too hard.
正如我们在那里看到的那样,此配置仅影响来自同一网站的 连续页面 ,这是因为已分配 slots
的抓取工具。默认情况下,scrapy
为每个域设置一个插槽(因为这个想法是每个插槽都应该处理自己的速度)。
现在,您还可以更改可以使用 meta
变量 download_slot
处理的 slot
请求,因此如果您不这样做,请确保您没有使用该变量不知道它能做什么。
其他设置也会干扰 DOWNLOAD_DELAY
,例如:
因此请确保它们未启用,或者您没有尝试在同一个项目中使用这两种设置。
另外需要指出的是,download_delay
也可以作为 Spider
变量启用,并且它优先于设置中的变量。
好的,我刚刚找到问题的答案。
它来自于我 运行 在 main.py 文件中创建 CrawlerProcess。它没有加载 settings.py 文件中的设置。
之前我做了以下事情:
process = CrawlerProcess({
'USER_AGENT': 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)'
})
process.crawl(fitbit_spider.FitbitSpider)
process.start()
现在,如果我将 CrawlerProcess 更改为:
process = CrawlerProcess({
'USER_AGENT': 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)',
'DOWNLOAD_DELAY': 20
})
我确实得到了想要的延迟!
注意:使用 get_project_settings()
创建 CrawlerProcess 也不起作用。