Skip to content
$ learn rotate-proxies

How to rotate proxies for web scraping

Rotation models, a working Python rotator, and keeping the pool healthy.

Sending every request from one IP address is the quickest way to get rate limited. Rotating proxies spreads requests across many addresses so no single one draws attention. This guide covers the two rotation models, a working Python implementation with retries and health checks, how to react to 403, 429 and CAPTCHA responses, and how to keep the pool clean. Addresses are documentation examples (203.0.113.x, 198.51.100.x).

Per-request rotation vs sticky sessions

There are two models, and the right one depends on whether the target tracks state.

Many providers offer a single gateway address that rotates for you, with a session parameter in the username to request a sticky IP. Check your vendor's documentation, since the syntax differs. If you manage your own list, you implement rotation in code, as below.

A simple rotator in Python

itertools.cycle gives round-robin rotation. Wrap it with timeouts and a retry that moves to the next proxy on failure:

import itertools
import time
import requests

PROXIES = [
    "http://203.0.113.10:8080",
    "http://203.0.113.11:8080",
    "http://198.51.100.7:3128",
]
pool = itertools.cycle(PROXIES)

def fetch(url, tries=4):
    for attempt in range(tries):
        proxy = next(pool)
        try:
            r = requests.get(
                url,
                proxies={"http": proxy, "https": proxy},
                timeout=(5, 15),
            )
            if r.status_code == 200:
                return r
            if r.status_code in (403, 429):
                time.sleep(2 ** attempt)   # back off, then try another IP
                continue
            r.raise_for_status()
        except requests.RequestException:
            time.sleep(2 ** attempt)
    raise RuntimeError("all attempts failed for " + url)

Use random.choice instead of cycle if you do not want a predictable order. For syntax details in other tools, see using a proxy with curl, Python and Node.js.

Sticky sessions with your own list

Bind a requests.Session to one proxy for the lifetime of a task:

def new_session():
    proxy = next(pool)
    s = requests.Session()
    s.proxies.update({"http": proxy, "https": proxy})
    return s

session = new_session()   # reuse for login + following pages

Create a fresh session only when the old one fails or the task ends.

Handling 403, 429 and CAPTCHA

ResponseUsual meaningReasonable reaction
429 Too Many RequestsYou exceeded a rate limitSlow down. Honor the Retry-After header if present, and add delay between requests, not just another IP.
403 ForbiddenThe IP, headers or behavior were rejectedTry another proxy, but also review headers and request pattern; a new IP will not fix a bad fingerprint.
CAPTCHA or challenge pageThe site suspects automationReduce rate, use sticky sessions, consider a higher-reputation IP type. Do not assume the HTTP status is an error; challenge pages often return 200.
407Proxy credentials rejectedNot a target block. See proxy error 407.
Timeout or connection resetDead or overloaded proxyRetry on another proxy and count the failure.

Rotation is not a substitute for politeness. Adding random delays, capping concurrency per site and caching pages you already fetched will reduce blocks more than adding IPs will. Detect challenge pages by content (for example a known marker string) and treat them as failures.

Health checks and removing dead proxies

Free and cheap proxies die constantly, and a pool that worked last week may be mostly dead today. Track failures per proxy and evict the ones that keep failing:

from collections import defaultdict

fails = defaultdict(int)
MAX_FAILS = 3

def mark_failure(proxy):
    fails[proxy] += 1
    if fails[proxy] >= MAX_FAILS and proxy in PROXIES:
        PROXIES.remove(proxy)

def mark_success(proxy):
    fails[proxy] = 0

If you use a plain list with cycle, rebuild the cycle after removing entries. Better still, validate the whole list before each run. Paste it into the proxy checker, keep the entries that respond and drop the rest, then feed the survivors to your scraper. Our checker tests HTTP, HTTPS and SOCKS5, so you also learn which scheme to use for each entry. See how to check if a proxy is working for what a pass or fail means.

Checks only tell you the proxy answered at that moment. Keep the in-code failure counting as well.

Ethics, robots.txt and terms of service

Technical ability is not permission. Before scraping a site:

When in doubt on legal questions, ask a qualified professional; this article is not legal advice.

Putting it together

A sensible workflow: validate the list, load the survivors, rotate per request for stateless pages or per session for stateful ones, back off on 429, count failures, and re-validate between runs. Log which proxy served each request together with the status code. When blocks rise, those logs show whether one subnet, one target or your own request rate is the cause, and that tells you whether to change proxies, slow down or change approach.

Common mistakes

Published and last updated 2026-12-02