scrapy-rotating-proxies
This package provides a Scrapy middleware to use rotating proxies, check that they are alive and adjust crawling speed.
License is MIT.
Installation
pip install scrapy-rotating-proxies
Usage
Add ROTATING_PROXY_LIST option with a list of proxies to settings.py:
ROTATING_PROXY_LIST = [ 'proxy1.com:8000', 'proxy2.com:8031', # ... ]
You can load it from file if needed:
def load_lines(path):
with open(path, 'rb') as f:
return [line.strip() for line in
f.read().decode('utf8').splitlines()
if line.strip()]
ROTATING_PROXY_LIST = load_lines('/my/path/proxies.txt')
Then add rotating_proxies middlewares to your DOWNLOADER_MIDDLEWARES:
DOWNLOADER_MIDDLEWARES = {
# ...
'rotating_proxies.middlewares.RotatingProxyMiddleware': 610,
'rotating_proxies.middlewares.BanDetectionMiddleware': 620,
# ...
}
Concurrency
By default, all default Scrapy concurrency options (DOWNLOAD_DELAY, AUTHTHROTTLE_..., CONCURRENT_REQUESTS_PER_DOMAIN, etc) become per-proxy for proxied requests when RotatingProxyMiddleware is enabled. For example, if you set CONCURRENT_REQUESTS_PER_DOMAIN=2 then spider will be making at most 2 concurrent connections to each proxy, regardless of request url domain.
Customization
scrapy-rotating-proxies keeps track of working and non-working proxies, and re-checks non-working from time to time.
Detection of a non-working proxy is site-specific. By default, scrapy-rotating-proxies uses a simple heuristic: if a response status code is not 200, response body is empty or if there was an exception then proxy is considered dead. To customize this with site-specific rules define response_is_ban and/or exception_is_ban spider methods:
class MySpider(scrapy.spider):
# ...
def response_is_ban(self, request, response):
return b'banned' in response.body
def exception_is_ban(self, request, exception):
return None
It is important to have these rules correct because action for a failed request and a bad proxy should be different: if it is a proxy to blame it makes sense to retry the request with a different proxy.
Non-working proxies could become alive again after some time. scrapy-rotating-proxies uses a randomized exponential backoff for these checks - first check happens soon, if it still fails then next check is delayed further, etc. Use ROTATING_PROXY_BACKOFF_BASE to adjust the initial delay (by default it is random, from0 to 5 minutes).
Settings
ROTATING_PROXY_LIST - a list of proxies to choose from;
ROTATING_PROXY_LOGSTATS_INTERVAL - stats logging interval in seconds, 30 by default;
ROTATING_PROXY_CLOSE_SPIDER - When True, spider is stopped if there are no alive proxies. If False (default), then when there is no alive proxies all dead proxies are re-checked.
ROTATING_PROXY_PAGE_RETRY_TIMES - a number of times to retry downloading a page using a different proxy. After this amount of retries failure is considered a page failure, not a proxy failure. Default: 15.
ROTATING_PROXY_BACKOFF_BASE - base backoff time, in seconds. Default is 300 (i.e. 5 min).
Contributing
source code: https://github.com/TeamHG-Memex/scrapy-rotating-proxies
bug tracker: https://github.com/TeamHG-Memex/scrapy-rotating-proxies/issues
To run tests, install tox and run tox from the source checkout.
CHANGES
0.1 (2016-02-01)
Initial release
Metadata
Release files for scrapy-rotating-proxies 0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| scrapy-rotating-proxies-0.1.tar.gz | 7.6 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| scrapy_rotating_proxies-0.1-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details |
Total release size: 18.2 kB
Release files / scrapy-rotating-proxies-0.1.tar.gz
| Download URL | scrapy-rotating-proxies-0.1.tar.gz |
|---|---|
| Size | 7.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ea9367665ababb48e0eaba80ccfa8a91215560969bbbab333254fef2c6757837
|
|
BLAKE2b-256 checksum How to use checksums |
ab5773ac54a31fac6a8f02160e80e7a90c6943efda0c9e7318a64809d7536fdb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / scrapy_rotating_proxies-0.1-py2.py3-none-any.whl
| Download URL | scrapy_rotating_proxies-0.1-py2.py3-none-any.whl |
|---|---|
| Size | 10.5 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
bd4c6b5f4b7819396172e57fa4069060b338e04da29102b0e71f9a03a1cf9613
|
|
BLAKE2b-256 checksum How to use checksums |
dd8393a1605c6f3a9247bca9a4fb02eaa881ad58aa932e5bbbafd9b325d87722
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for scrapy-rotating-proxies 0.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.