scrapy-proxy-headers 0.3.0 runs on Scrapy 2.20 and newer. If you run Scrapy 2.17 or later on Python 3.10 or newer, install 0.3.0. If you must stay on Python 3.8 or 3.9, stay on 0.2.2.
pip install -U scrapy-proxy-headers
Requirements: Python 3.10+ and Scrapy 2.17+
0.3.0 requires Python 3.10 or newer, matching Scrapy 2.17.0, and
scrapy>=2.17.0 with no upper cap. 0.2.2 is the last release for Python 3.8
and 3.9. Scrapy 2.20.0 and Scrapy master require Python 3.11 or newer, because
Scrapy 2.20
dropped support for Python 3.10.
Scrapy 2.20 support
0.3.0 works with the proxy headers support built into
Scrapy 2.20.0.
You send headers the same way, with proxy_headers in your request meta, and
you still get the X-ProxyMesh-IP response header, which Scrapy alone doesn't
give you.
Keep DOWNLOAD_HANDLERS set to scrapy_proxy_headers.HTTP11ProxyDownloadHandler
if your spider reads X-ProxyMesh-IP from response.headers. On Scrapy 2.20
and newer, a project that only sends proxy_headers can omit the handler and
keep the same meta value.
Different headers stay separate
0.3.0 keeps X-ProxyMesh-IP matched to the proxy_headers you sent, so
requests with different headers never share another request's value. On Scrapy
2.17 through 2.19, each set of proxy_headers also gets its own connection to
the proxy. Those Scrapy releases otherwise reuse one connection per proxy, so a
later request with different headers could skip its own.
Example: choose a country and read the proxy IP
Scrapy 2.16 removed start_requests(), so a spider that only defines it sends
no requests on any Scrapy version 0.3.0 supports. Define async def start().
import scrapy
class CountrySpider(scrapy.Spider):
name = "country"
custom_settings = {
"DOWNLOAD_HANDLERS": {
"https": "scrapy_proxy_headers.HTTP11ProxyDownloadHandler",
},
}
async def start(self):
yield scrapy.Request(
"https://api.ipify.org?format=json",
meta={
# X-ProxyMesh-Country works on the World and Open proxies.
"proxy": "http://USERNAME:PASSWORD@PROXY_HOST:PORT",
"proxy_headers": {"X-ProxyMesh-Country": "DE"},
},
)
def parse(self, response):
proxy_ip = response.headers.get(b"X-ProxyMesh-IP")
yield {"proxy_ip": proxy_ip.decode() if proxy_ip else None}
Only some ProxyMesh proxies honor X-ProxyMesh-Country. The
request and response headers article
lists every header ProxyMesh accepts and returns, and the
Python proxy configuration article
covers the Scrapy setup.
Install it from scrapy-proxy-headers 0.3.0 on PyPI. The GitHub release notes and the full diff from 0.2.2 list the changes. Usage is covered in the scrapy-proxy-headers documentation.