Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scrapy delay request

every time i run my code my ip gets banned. I need help to delay each request for 10 seconds. I've tried to place DOWNLOAD_DELAY in code but it gives no results. Any help is appreciated.

# item class included here
        class DmozItem(scrapy.Item):
            # define the fields for your item here like:
            link = scrapy.Field()
            attr = scrapy.Field()


        class DmozSpider(scrapy.Spider):
            name = "dmoz"
            allowed_domains = ["craigslist.org"]
            start_urls = [
            "https://washingtondc.craigslist.org/search/fua"
            ]

            BASE_URL = 'https://washingtondc.craigslist.org/'

            def parse(self, response):
                links = response.xpath('//a[@class="hdrlnk"]/@href').extract()
                for link in links:
                    absolute_url = self.BASE_URL + link
                    yield scrapy.Request(absolute_url, callback=self.parse_attr)

            def parse_attr(self, response):
                match = re.search(r"(\w+)\.html", response.url)
                if match:
                    item_id = match.group(1)
                    url = self.BASE_URL + "reply/nos/vgm/" + item_id

                    item = DmozItem()
                    item["link"] = response.url

                    return scrapy.Request(url, meta={'item': item}, callback=self.parse_contact)

            def parse_contact(self, response):
                item = response.meta['item']
                item["attr"] = "".join(response.xpath("//div[@class='anonemail']//text()").extract())
                return item
like image 497
Arkan Kalu Avatar asked May 22 '15 19:05

Arkan Kalu


1 Answers

You need to set DOWNLOAD_DELAY in settings.py of your project. Note that you may also need to limit concurrency. By default concurrency is 8 so you are hitting website with 8 simultaneous requests.

# settings.py
DOWNLOAD_DELAY = 1
CONCURRENT_REQUESTS_PER_DOMAIN = 2

Starting with Scrapy 1.0 you can also place custom settings in spider, so you could do something like this:

class DmozSpider(Spider):
    name = "dmoz"
    allowed_domains = ["dmoz.org"]
    start_urls = [
        "http://www.dmoz.org/Computers/Programming/Languages/Python/Books/",
        "http://www.dmoz.org/Computers/Programming/Languages/Python/Resources/",
    ]

    custom_settings = {
        "DOWNLOAD_DELAY": 5,
        "CONCURRENT_REQUESTS_PER_DOMAIN": 2
    }

Delay and concurrency are set per downloader slot not per requests. To actually check what download you have you could try something like this

def parse(self, response):
    """
    """
    delay = self.crawler.engine.downloader.slots["www.dmoz.org"].delay
    concurrency = self.crawler.engine.downloader.slots["www.dmoz.org"].concurrency
    self.log("Delay {}, concurrency {} for request {}".format(delay, concurrency, response.request))
    return
like image 58
Pawel Miech Avatar answered Oct 16 '22 18:10

Pawel Miech