Home/Docs/Scrapy Proxy Rotation

Scrapy Proxy Rotation Done Right

per-request meta · ban-proof middleware · retries & AutoThrottle · updated 6 November 2026
Tutorials
Home › Tutorials › Scrapy Proxy Rotation Done Right

Scrapy sends every request through HttpProxyMiddleware, which reads the proxy from the request's meta. Per-request assignment looks like this:

yield scrapy.Request(
    url,
    meta={"proxy": "http://USER:PASS@GATEWAY:PORT"},
)

With a rotating gateway that is all you need — the provider swaps exit IPs server-side. The rest of this guide is about doing rotation yourself when you have several gateways, and surviving bans.

A minimal rotating middleware

Add this middleware and it assigns a gateway to every request that has none:

# middlewares.py
import random

GATEWAYS = [
    "http://USER:PASS@GW1:PORT",
    "http://USER:PASS@GW2:PORT",
]

class RandomProxyMiddleware:
    def process_request(self, request, spider):
        if "proxy" not in request.meta:
            request.meta["proxy"] = random.choice(GATEWAYS)
# settings.py
DOWNLOADER_MIDDLEWARES = {
    "myproject.middlewares.RandomProxyMiddleware": 760,  # HttpProxyMiddleware is 750 — stay after it
}
Order matters: HttpProxyMiddleware is at 750. Register yours at 750+1 or higher so your meta["proxy"] assignment is what the built-in middleware reads. If you set a lower number, you will fight it.

Retry with a different IP on ban

Scrapy's RetryMiddleware retries the same request — same proxy, same fate. Extend it to drop the proxy on ban-flavoured failures:

from scrapy.downloadermiddlewares.retry import RetryMiddleware

class BanAwareRetryMiddleware(RetryMiddleware):
    def process_response(self, request, response, spider):
        if response.status in (403, 429) and request.meta.get("proxy"):
            request.meta["proxy"] = random.choice(GATEWAYS)  # force a new gate/IP
        return super().process_response(request, response, spider)

    def process_exception(self, request, exception, spider):
        if isinstance(exception, (TimeoutError, ConnectionError)) and request.meta.get("proxy"):
            request.meta["proxy"] = random.choice(GATEWAYS)
        return super().process_exception(request, exception, spider)

Also raise the retry budget for hostile targets:

RETRY_ENABLED = True
RETRY_TIMES = 4
RETRY_HTTP_CODES = [500, 502, 503, 504, 403, 429, 408]

Sticky sessions per spider or per user

When a site ties content to session cookies, the same exit IP must serve the whole flow. Most residential gateways accept a session token in the username:

def sticky_proxy(sid: str) -> str:
    return "http://USER-session-%s:PASS@GATEWAY:PORT" % sid

request.meta["proxy"] = sticky_proxy(spider.name + "-%d" % (hash(request.url) % 1000))

SOCKS5 in Scrapy

Install the extra and use socks5h:// so DNS resolves on the proxy side:

pip install scrapy[socks]
request.meta["proxy"] = "socks5h://USER:PASS@GATEWAY:PORT"

Throttle for residential pools

Residential exits drop and renumber constantly. AutoThrottle plus generous timeouts keeps the crawl alive:

AUTOTHROTTLE_ENABLED = True
AUTOTHROTTLE_START_DELAY = 1.0
AUTOTHROTTLE_MAX_DELAY = 15
DOWNLOAD_TIMEOUT = 30
CONCURRENT_REQUESTS = 16
CONCURRENT_REQUESTS_PER_DOMAIN = 4

Debugging checklist

FAQ

Does Scrapy rotate proxies automatically?

No. Scrapy reads the proxy from request.meta via HttpProxyMiddleware but never changes it. Rotation is either done by a rotating gateway server-side or by your own middleware assigning different gateways per request.

What order should my proxy middleware use?

Register your middleware at 751 or higher. HttpProxyMiddleware sits at 750, and a later process_request can set or overwrite request.meta['proxy'] before the built-in middleware consumes it.

How do I avoid reusing a banned IP?

Extend RetryMiddleware: on 403/429 responses or connection exceptions, replace request.meta['proxy'] before the retry fires, and add a session token so the gateway allocates a fresh exit IP.

Can Scrapy use SOCKS5 proxies?

Yes, after pip install scrapy[socks]. Use socks5h:// so DNS resolves on the proxy side and you avoid DNS leaks.

Need a pool to rotate against? IPLight's rotating residential gateway gives you 170+ countries from $1.20/GB — see pricing, or check the live pool report first.

Test on your own target list

Residential from $1.20/GB · static ISP from $4/IP per month · no monthly commitment. Create an account, top up a small balance and run your first requests in minutes.

Create account

Frequently asked questions

How much does it cost?

Dynamic residential traffic starts at $1.20/GB on the 100 GB tier ($120), dropping to $0.96/GB at 1,000 GB, $0.72/GB at 5,000 GB and $0.60/GB at 10,000 GB. Static residential IPs are $4/IP/month, or $4/IP/month. There are no setup fees or monthly platform fees.

Does traffic count both upload and download?

Yes - traffic is metered in both directions and 1 GB = 1,000 MB. The console shows an hourly usage breakdown per sub-account, so you can audit exactly what each job consumed. Disabling images and video in your crawler cuts usage significantly.

Is there a contract or minimum commitment?

No. Top up once and spend the balance across dynamic traffic, static IPs and unlocking. Paid orders keep their unit price even if the public price list changes later.

Which protocols are supported?

HTTP and SOCKS5 endpoints are provided, with username/password or IP-whitelist authentication. AdsPower, BitBrowser, Python, Node, Scrapy, Playwright and Puppeteer all connect directly.

What is the difference between ISP proxies and static residential proxies?

At IPLight they are the same product: real IP addresses registered to consumer internet service providers, dedicated to your account for the whole term. Some providers sell 'ISP proxies' that are actually datacenter ranges announced by an ISP - ours are checked as residential before they enter inventory.

Can I keep the same IP for months?

Yes. Choose a 30, 90 or 180-day term and the same IP stays assigned to your account for the full term, which is what makes ISP proxies suitable for account management and long-lived logins.

Popular proxy locations: All locations · USA · UK · Germany · Japan · Indonesia · Brazil · India · Russia
Popular products: Residential proxies · ISP proxies · SERP proxies · Static residential · Pricing
IPLight live residential pool: 215,000+ online IPs across 170+ countries & regions · dynamic residential from $1.20/GB · pool sizes measured in real time · updated 2026-11