Short answer
Pace an authorized scraper by making the client polite by default: one shared rate limiter, a concurrency cap, retries that honor Retry-After and exponential backoff, a cache so you do not refetch unchanged pages, and a hard stop when the site returns 429, 403, or a terms/robots disallow. Build this in Python around Playwright or HTTPX; use Rust only if you need a fast shared limiter or cache keyed on normalized URLs. Fair-use pacing is about staying inside the site’s published limits and your written authorization — not about looking like a human or bypassing a block.
Scope
This article is for sources you are allowed to collect: public APIs, official bulk downloads, or sites you have permission to access. It is not a guide to evading bot detection, rotating identities to dodge blocks, or ignoring robots.txt and terms of use.
If a target suspends or blocks you, stop and review the authorization and the rate. Do not add an automatic “recover and continue” path.
The pieces that matter
-
A single limiter for the whole process, not a
sleep()in one function and a free-for-all in another. - Concurrency limits. Two or four workers are often enough. More workers can mean more 429s, not more throughput.
- Cache first. If you already have a fresh enough copy, do not hit the network.
-
Backoff that listens. Honor
Retry-After. Back off on 429 and 5xx. Give up after a fixed number of attempts and record the failure. - A budget. Max requests per minute and per hour, with a default that is conservative. Make the cap configurable, and log when you hit it.
A shared limiter in Python
import random, threading, time
from datetime import datetime, timezone
class RateLimiter:
def __init__(self, per_minute: int = 20, per_hour: int = 400):
self.per_minute = per_minute
self.per_hour = per_hour
self._hits: list[float] = []
self._lock = threading.Lock()
def acquire(self) -> None:
while True:
with self._lock:
now = time.monotonic()
self._hits = [t for t in self._hits if now - t < 3600]
last_min = sum(1 for t in self._hits if now - t < 60)
last_hour = len(self._hits)
if last_min < self.per_minute and last_hour < self.per_hour:
self._hits.append(now)
return
wait = 60.0 if last_min >= self.per_minute else 3600.0 - (now - self._hits[0])
time.sleep(min(wait, 30) + random.uniform(0, 0.5))
The jitter is only there to avoid a thundering herd when several workers wake at once. It is not a disguise.
Honor Retry-After and stop on a hard refusal
import time
import httpx
def fetch(client: httpx.Client, limiter: RateLimiter, url: str, attempts: int = 5):
for n in range(attempts):
limiter.acquire()
r = client.get(url, timeout=20, headers={"User-Agent": "ExampleCollector/1.0 (+contact@example.com)"})
if r.status_code in (401, 403, 451):
raise RuntimeError(f"stopped: HTTP {r.status_code} for {url}")
if r.status_code == 429 or r.status_code >= 500:
retry_after = r.headers.get("Retry-After")
try:
delay = float(retry_after) if retry_after else 2 ** n
except ValueError:
delay = 2 ** n
time.sleep(min(delay, 120))
continue
r.raise_for_status()
return r
raise RuntimeError(f"gave up after {attempts} attempts: {url}")
Identify your client honestly. Include a contact address in the user-agent if the project is long-running. Caching (ETag / If-None-Match, or a local table of URL plus fetched-at) belongs in front of fetch.
Check robots and your written scope before the first request
- Read the site’s robots.txt and any published API limit. If a path is disallowed, skip it and log the skip.
- Stay on the documented API or the pages you were authorized to use. Official bulk files (for example an FMCSA census download) are usually a better first choice than a page scrape.
- Store a run log: start time, request count, 429 count, and when the limiter paused. That log is how you show the job was paced, and how you tune the cap downward if needed.
A small Rust helper can sit behind this if you want a process-wide counter that several Python workers share through a file or Redis-compatible store. The policy still lives in one place: requests per minute, requests per hour, and “stop on 403.”
When the site says no
Pause the job. Alert the operator. Do not rotate IPs, accounts, or sessions to continue. Recovery, if it happens at all, is a manual review of authorization, rate, and the site’s current terms.
FAQ
What default limits should I use?
There is no universal number. Start from the site’s documented cap if it has one. If it does not, start low (for example tens of requests per minute, not hundreds) and only raise the cap when the run log stays clean. This article does not publish a “safe” rate that will keep a site unblocked.
Is a random delay the same as fair use?
No. A random sleep does not replace a real budget, a concurrency cap, or Retry-After. Random delay is not a way to look like a person.
Should I use Playwright or a plain HTTP client?
Use a plain HTTP client when the data is in the response HTML or JSON. Use Playwright when the authorized source actually requires a browser. A browser is heavier; it still must go through the same limiter.
Can Rust help here?
Yes, as a shared limiter or a cache keyed on normalized URLs, especially if several workers run on one host. Python can still own the crawler, the parsers, and the run log.
What about personal data?
Collect only what you are allowed to store, keep a retention limit, and do not log raw personal fields in world-readable run files.
What this is not
This is not advice on bypassing access controls, CAPTCHAs, or blocks, and it does not claim that pacing will keep a SIM, IP, or account unblocked.
I'm Muhammad Afzal, a Rust and Python automation engineer at INDUS Web Agency.
Top comments (0)