首页 > 解决方案 > 如果一个代理很好,那么如何只坚持该代理直到被禁止然后移动到另一个代理,在scrapy-proxy-rotation中?

问题描述

昨天,我问了一个问题,从答案中我发现我需要使用代理来抓取该网站。所以我在那个脚本中实现了scrapy-rotation-proxy 。

这是更改后的 settings.py

ROTATING_PROXY_LIST_PATH = '/my/path/proxies.txt'

DOWNLOADER_MIDDLEWARES = {
    'rotating_proxies.middlewares.RotatingProxyMiddleware': 610,
    'rotating_proxies.middlewares.BanDetectionMiddleware': 620,
}

ROBOTSTXT_OBEY = False

在实现了所有这些之后,在抓取了大约 370 多页之后,scrapy 仍然停止。由于我是轮换代理的新手,我想知道如何只坚持一个代理/ip(以防万一),直到它被禁止,然后再轮换到 proxies.txt 文件中的另一个代理/ip。因为我注意到如果代理很好,所需的数据会被抓取,如下所示

2019-10-06 12:50:11 [rotating_proxies.expire] DEBUG: Proxy <http://197.254.16.30:8080> is DEAD
2019-10-06 12:50:11 [rotating_proxies.middlewares] DEBUG: Retrying <GET https://www.gulahmedshop.com/khadi-net-3-pc-outfit-glamour-19-48> with another proxy (failed 3 times, max retries: 5)
2019-10-06 12:50:12 [rotating_proxies.expire] DEBUG: Proxy <http://181.30.95.162:33078> is GOOD
2019-10-06 12:50:12 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://www.gulahmedshop.com/gls-18-143> (referer: https://www.gulahmedshop.com/women?cat=399&price=-3000)
2019-10-06 12:50:12 [scrapy.core.scraper] DEBUG: Scraped from <200 https://www.gulahmedshop.com/gls-18-143>
{'Image Url': u'https://d224nth7ac0evy.cloudfront.net/catalog/product/cache/1e8ef93b9b4867ab9f3538dde2cb3b8a/g/l/gls-18-143_1_.jpg', 'Price': u'PKR 2,058', 'Category Name': u'and above', 'Product Title': u'GLS-18-143', 'Prouct page': 'https://www.gulahmedshop.com/gls-18-143'}
2019-10-06 12:50:12 [rotating_proxies.expire] DEBUG: Proxy <http://171.239.46.185:8080> is DEAD
2019-10-06 12:50:12 [rotating_proxies.middlewares] DEBUG: Retrying <GET https://www.gulahmedshop.com/gls-18-230> with another proxy (failed 2 times, max retries: 5)

标签: python-2.7scrapy

解决方案


除非您需要持久会话,否则您不想这样做。

您希望请求分布在不同的代理之间,以便每个代理每分钟的请求量尽可能低,从而降低它们受到限制的可能性。

如果您使用免费代理,您需要更多免费代理,切换到付费代理,或者更好的是单个智能代理

首先弄清楚为什么你被禁止也是一个好主意,但这可能很难做到。


推荐阅读