All articles3 min read
Implementation guides

HTTPX proxy setup for Python scrapers

Configure a supported Python client with current proxy and TLS behavior.

A developer works at a laptop with a wired network adapter.

HTTPX proxy setup should make the route explicit and keep authentication private. Start with one permitted target, one assigned proxy URL and a response check you can explain. Add concurrency or retries after that basic path works.

Keep the two URLs straight

The destination URL identifies the page you want. The proxy URL identifies the intermediary your client connects to. Their schemes describe different connections. An HTTPS destination does not, by itself, tell you which scheme the proxy endpoint supports.

Use the proxy address supplied by the account and the interface supported by your installed HTTPX version. Current HTTPX configuration uses the singular proxy argument. Old snippets using a different constructor contract need review before reuse.

Make the route explicit

This function retrieves a permitted document through an assigned proxy. It turns off inherited environment routing for a predictable example and leaves certificate verification at its normal secure default.

import httpx

def read_document(url, proxy_url):
    timeout = httpx.Timeout(15.0, connect=5.0, pool=2.0)
    with httpx.Client(
        proxy=proxy_url,
        trust_env=False,
        timeout=timeout,
        follow_redirects=False,
    ) as client:
        response = client.get(url)
        response.raise_for_status()
        return response.text

Load both values from approved private configuration. The example returns response text, not a validated dataset. It does not impose a response-size cap or a complete job deadline. Add those application controls where the workload needs them.

Protect credentials and route evidence

If the account supplies a credential-bearing URL, keep it out of repository files, exception logs and screenshots. Encoding reserved characters correctly matters when constructing a URL from separate values. Prefer an account-generated configuration when available.

Keep proxy authentication separate from target authentication. A target's authorization header does not establish the proxy login. Record sanitized evidence that the route was used, without exposing the secret that enabled it.

When production intentionally uses environment settings, test that path explicitly. Do not assume it behaves like this function with trust_env=False. A bypass entry or inherited setting can change the route while still returning a plausible page.

Interpret timeouts by phase

A connect timeout, a wait for a pooled connection and a slow response read are different failure stages. Keep the exception category with the request record. Raising every timeout value can hide the stage that needs attention.

These client timeouts do not create one end-to-end deadline for all retries and queued work. Define a job budget outside the function. Stop new attempts when that budget is exhausted rather than extending it with each retry.

Check more than transport success

Inspect the final response and expected page identity. The example disables automatic redirect following so that the caller can decide whether a new destination remains in scope. Validate any redirect destination before following it.

Keep an authentication failure, target response and invalid extracted record as different outcomes. A returned page can be a consent screen or another unexpected template. Do not mark the job successful until its required data passes the record contract.

Use a small reproducible test

A local proxy fixture can establish how the client sends the request and authentication. Include a valid response, a rejected login and a slow-response case. Label those tests as fixtures; they do not prove a production provider's target coverage.

Check the actual installed library version and current residential proxy settings before moving the client into a larger scraper. Prepare one limited route test with a permitted target, then retain the sanitized result with the deployment configuration.

Contact us

Tell us what you want to collect.

See our Privacy Notice for how we handle your message.

Your message is sent to hello@crawlhub.ai. Please do not include passwords or API keys.