How to resolve the same request n times more?

When I request a server, it sometimes responds to a web page that is not the one expected, even when the response status is 200. I know that I can use this method to request the same URL several times:

def parse(response):
    try:
        # parsing logic here
    except AttributeError:
        yield Request(response.url, callback=self.parse, dont_filter=True)

But how could one limit the number of times, say, 10 times, that the same url can be requested to avoid endless loops when the web page is really what it is?

+4
source share
1 answer

, functools.partial, parse . ( 10), yield Request. :

from functools import partial

def parse(response,ntimes=0):
    try:
        # parsing logic here
        pass
    except AttributeError:
        if ntimes < 10:
            yield Request(response.url, callback=partial(self.parse,ntimes=ntimes+1), dont_filter=True)

, parse partial(..) , ntimes ntimes+1 ( "" , ). ntimes 10 , .

ntimes=0 0, parse , ( parse "" URL- ).

+1
source

Source: https://habr.com/ru/post/1668293/


All Articles