aivarsk / scrapy-proxies

Random proxy middleware for Scrapy
MIT License
1.66k stars 409 forks source link

Passing proxy via meta in start request throws KeyError: 'exception' #54

Open superryeti opened 5 years ago

superryeti commented 5 years ago

I see this is caused by the line no 83.

 if 'proxy' in request.meta:
            if request.meta["exception"] is False:
                return

If we have used proxy in start requests function then this issue arises which makes sense because exception is not defined in meta up to this point for our first request.

I guess most of us either use a random proxy or custom proxy. So no one ever bothered about it. I think the line 83 is important because it enables to change proxies in each retry or after exception.

def start_requests(self):

        yield scrapy.Request('http://quotes.toscrape.com/', callback=self.parse, meta={'proxy': 'http://xxxx:xxxx@xxxx:xxxx'})

Also to change the proxy in retry. Comment out this in process_exception. #15

       if 'proxy' not in request.meta:
             return
superryeti commented 5 years ago

This solves the issue. Now we can pass the proxy in our first request via meta. and our script would still handle retries with new proxies

if 'proxy' in request.meta:
            if request.meta.get("exception", True):
                log.info('Using custom proxy provided in meta')
                    return