Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python scrapy, how to do a random delay between each request in a single spider?

Tags:

python

scrapy

I have a spider where i want to have a delay between each request, but i don't want it to be a fixed number rather a random amount of time between two bounds, how can i do that ?

like image 494
user2968505 Avatar asked Jan 04 '23 15:01

user2968505


1 Answers

First of all, avoid to use time.sleep because it blocks Twisted reactor and thus eliminates all the advantages of Scrapy concurrency.

By default, Scrapy's DOWNLOAD_DELAY setting adds random delay in range between 0.5 * DOWNLOAD_DELAY and 1.5 * DOWNLOAD_DELAY seconds. This behaviour manages by RANDOMIZE_DOWNLOAD_DELAY setting which is true by default.

Docs: https://doc.scrapy.org/en/latest/topics/settings.html#std:setting-DOWNLOAD_DELAY

like image 152
mizhgun Avatar answered Feb 12 '23 17:02

mizhgun