Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update proxy list once a day #53

Open
mo-tech55 opened this issue Nov 3, 2020 · 1 comment
Open

Update proxy list once a day #53

mo-tech55 opened this issue Nov 3, 2020 · 1 comment

Comments

@mo-tech55
Copy link

mo-tech55 commented Nov 3, 2020

I use a proxy list from a proxy provider and my proxy list gets renewed once a day. I get the proxy list from the provider via their api.

settings.py:

ROTATING_PROXY_LIST = proxy_list()
DOWNLOADER_MIDDLEWARES = {
'rotating_proxies.middlewares.RotatingProxyMiddleware': 610,
'rotating_proxies.middlewares.BanDetectionMiddleware': 620,
}

def proxy_list() -> list:
    response = requests.get('https://proxy.webshare.io/api/proxy/list', headers={"Authorization":f"Token{API_KEY}"}).json()
    proxy_results = [ f'{proxy_elem["proxy_address"]}:{proxy_elem["ports"]["http"]}' for proxy_elem in response['results']]
    return proxy_results

The above code works but I think scrapy executes my proxy_list method just once, when I start my spider.

I keep my spider running in the spider.py file with following code:

def sleeping(self, *args, seconds):
    '''Non blocking sleep callback'''
    return deferLater(reactor, seconds, lambda: None)

process = CrawlerProcess(get_project_settings())
def _crawl(result, spider, sleeptime=60):
     deferred = process.crawl(spider)
    deferred.addCallback(lambda results: print(f'waiting {sleeptime} seconds before restart...'))
    deferred.addCallback(sleeping, seconds=sleeptime)
    deferred.addCallback(_crawl, spider)
    return deferred

if __name__ == "__main__":
    _crawl(None, AmazonSpider, sleeptime=1800)
    process.start()

So the question is how to tell scrapy to update my proxy list every few hours.

@rajatshenoy56
Copy link

See issue number #40 for the solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants