Originally published on proxymint.
Set a Scrapy proxy with meta or a middleware, then fix what breaks: tunnels that pin one exit, 407 TunnelErrors, per-domain concurrency and retries.
TL;DR
- A Scrapy proxy is one meta key. Set request.meta["proxy"] to http://USERNAME:PASSWORD@HOST:PORT, or set it on every request from a downloader middleware at priority 350. The built-in HttpProxyMiddleware does the rest.
- Kept-alive tunnels pin the exit. Scrapy reuses CONNECT tunnels for HTTPS, and every request inside one tunnel leaves from the same IP. In a local lab, 20 requests at template settings used 1 tunnel; a Connection: close header gave 20.
- A 407 on an HTTPS page is a TunnelError, retried twice. TunnelError is in the default retry list, so bad credentials cost three attempts per URL. On a plain http:// page the 407 is a response that is dropped after one attempt.
- CONCURRENT_REQUESTS_PER_IP is deprecated and never meant the proxy. It keyed limits on the target's resolved IP. Scrapy 2.14.0 deprecated it; per-domain limits and AutoThrottle are what protect the site.
- Scrapy retries 429 at once and ignores Retry-After. A small middleware that raises the domain's delay and retries once turns rate-limit responses into a slower crawl instead of extra traffic.
Key takeaways
- Build the proxy URL in a middleware from environment variables. Encode username and password with quote(value, safe=""), keep the scheme http:// for every page, and use setdefault so a spider can still override it.
- Choose reuse or a new exit per request on purpose. Leave keep-alive on for sessions; with a per-request rotation order, add Connection: close so every page opens its own tunnel, and accept a TLS handshake per page for it.
- Run the 20-request exit check before a long crawl. Count distinct IPs from an IP echo endpoint with and without Connection: close, so rotation is measured rather than assumed.
- Take 429 and 503 out of RETRY_HTTP_CODES. Handle them with a slow-down middleware, keep 403 and challenge pages out of every retry list, and stop when a site refuses the crawl.
- Track MB per 1,000 items from downloader/response_bytes. Compare it across settings changes, and turn on HTTPCACHE_ENABLED during development so repeat runs move no proxy traffic.
Read the full guide on proxymint, with the commands, tables and sources.
Top comments (0)