Building Resilient Web Scrapers: How to Handle Cloudflare, Captchas, and Rate Limits with Playwright in 2026

Every scraper works on the first day. The test comes later: the site changes its layout, the server answers 429, the network drops mid-run, and a page that took two seconds yesterday takes thirty today. A scraper that cannot handle those days is not a pipeline — it is a demo that needs a babysitter.
Resilience is a ladder, climbed one rung at a time: the bottom rung is not scraping at all, and the top rung is knowing when to stop. One hard line up front: nothing here helps you solve a captcha or slip past a bot wall. If a site tells you to stop, you stop.
Rung one: use the official API first
Before writing a single selector, check for an official API, a data feed, or a downloadable export. An API is a contract: documented fields, a stated rate limit, an error format you can code against. Scraping is a guess — markup that can change without notice. When the data exists behind an API, use the API.
Our LeadForge Engine works this way: a Google Places scraper and a Playwright auditor, but a documented feed wins wherever one exists — the scraper is the fallback, never the first choice. Data behind a login or paywall is a permission problem, not a technical one, so we say no — or the client supplies it through their own account.
Rung two: read robots.txt and crawl like a guest
The next rung is politeness, and it starts with robots.txt — example.com/robots.txt — which says which paths the owner does not want crawled and how long to wait between requests. Reading it takes one request. Ignoring it is how a polite project turns into a block.
Send a real user-agent that identifies your project and includes a contact address. Keep a pause between requests — a second or two is a reasonable default — and never run ten parallel workers against a site that asked for a delay. Collect only the fields the brief needs: fetching a page nobody reads is pure load on someone else's server. A slow, identified visitor rarely meets a rate limit; a hundred anonymous requests a second always does.
Rung three: back off when the server says slow down
Even a polite crawler meets a 429 sometimes. Do not try again immediately — that is what caused the limit. Back off, and back off further each time: wait a short time, then double it, then double it again, up to a cap. When the server sends a Retry-After header, honour its value instead of your own guess.
Here is the retry helper we build scrapers around — it retries only 429, 502, 503, 504 and timeouts, and gives up cleanly on a 404 or a 403:
import asyncio
import random
from playwright.async_api import async_playwright, TimeoutError as PlaywrightTimeout
RETRYABLE = {429, 502, 503, 504}
async def fetch_with_backoff(page, url, attempts=4, base_delay=2.0):
"""Fetch a URL with exponential backoff. Returns the response,
or None when the page is gone, forbidden, or still failing."""
for attempt in range(attempts):
try:
response = await page.goto(url, wait_until="domcontentloaded", timeout=30000)
if response is None:
return None
if response.status in RETRYABLE:
retry_after = response.headers.get("retry-after")
wait = float(retry_after) if retry_after else base_delay * (2 ** attempt)
wait += random.uniform(0, 1) # jitter: stops retries syncing up
await asyncio.sleep(min(wait, 120))
continue
return response
except PlaywrightTimeout:
await asyncio.sleep(min(base_delay * (2 ** attempt), 120))
return None
Three details matter: the jitter — a random second per wait — stops retrying workers hammering the server at the same instant; the 120-second cap stops a wait growing into an hour; and the four-attempt limit means a dead page gets logged for a human instead of looping forever. A retry loop without a limit is not resilience — it is a denial-of-service attack you aimed at yourself.
Rung four: sessions, timeouts, and pages that change shape
Playwright runs the browser, but you manage the session. A browser context holds cookies and storage, so a site that sets a session cookie on the first visit expects it on the second — open a fresh context for every page and you look like a new visitor each time. Reuse one context per run, and close the browser when the run ends.
Every goto and locator wait should carry an explicit timeout — thirty seconds is our default — so a hung page fails loudly instead of freezing the run. When a page loads but the data is not where the selector expects it, log exactly what was expected and what the page contained. A parser that crashes silently leaves an empty sheet and no clue; one that logs "expected .price, found no match on this URL" tells you the layout changed.

Rung five: fail gracefully, and say so out loud
The top rung is the one most scrapers skip: a plan for the day the data does not come. Graceful failure means the run finishes anyway, the rows it did collect are stored, the rows it did not are listed by URL with the reason each failed, and a person is told.
In practice this is a dead-letter list: failed URLs go into a queue of their own, with the status code, the time, and the error text. A few retries happen automatically on the next scheduled run. Anything still failing after that is a human decision — the layout changed, the source moved, or the site now blocks automated access. Then tell the client the source is closed and offer the alternatives: the official API, a data vendor, or the client supplying the data.
What we never do
This needs saying plainly: we do not solve captchas, we do not use services that solve them for us, and we do not disguise traffic to get around a bot wall. A captcha is a site owner saying "prove you are a person", and automating around that is the thing the site is trying to prevent — in many places it can also be a legal problem. The same goes for rotating identities to dodge a block: the block is the answer, so ask for permission or use another source.
Bypass tricks are brittle: the site changes its challenge, your trick breaks, and you have burned the relationship with the source. A pipeline built on permission keeps working for years. One built on evasion works until Tuesday.
Putting the ladder together
A resilient scraper is a short checklist in order: official API before scraping, robots.txt before the first request, a pause between requests, exponential backoff with jitter and a cap, explicit timeouts, one browser context per run, a log that names what changed, and a dead-letter list with a human at the end of it. It is the same order as in Deploying Scalable Python Workflows & Scraping — collect, parse, queue, store, schedule, deploy, monitor — and this ladder decides whether the collector survives the real internet.
What to do next
If your team still copies data by hand, or your last scraper died the week after it was delivered, write down the source, the fields, and how often you need them. Python Automation starts at ₹7,999: start with our Python Automation service, get an instant estimate, or tell us the source on WhatsApp.