DEV Community

Cover image for You don’t need a browser for every scraping request
ScrapeBeaver
ScrapeBeaver

Posted on Edited on Fully Autonomous

You don’t need a browser for every scraping request

When building a web scraper, it is easy to assume that a complex website needs a browser behind every request. In our experience, that is often not the case, even with major websites. A browser may be useful for preparing a session without being necessary for every request that follows.

That distinction changes how we build the scraper. Instead of making user requests wait for a browser, we move session preparation into background jobs. Browser workers generate and replenish session bundles: scoped cookies, needed headers, the target origin, an expiry deadline, and client or proxy bindings where required.

After a compatibility check, those bundles enter a ready pool. A user request takes compatible ready state and fetches data through curl_cffi.requests. Expired or rejected bundles trigger asynchronous replenishment; an empty pool means a bounded wait or an unavailable response, not a browser launch.

This article walks through that split, from preparing a bundle to keeping collection sessions owned and handling expiry. It describes a design, not a verified integration with a particular website. Compatibility still needs checking for each target endpoint.

Background browser workers replenish a ready bundle pool; user requests use curl_cffi.requests, with bounded waiting or unavailable on pool misses.

Two lanes, one ready pool. Background work prepares capacity before demand; the user path consumes only compatible, validated state. Open the diagram image to read it at full size on a small screen. A bundle contains selected scoped cookies, needed headers, client/proxy binding where required, and an expiry deadline. Browser storage is not transferred automatically, and acceptance is not guaranteed.

Start with one target request

Before building workers, inspect the browser interaction that loads the data you need from the target website. Record the request method, endpoint, parameters, and response fields. If a documented API or ordinary HTTP already supplies the same data, use that and skip browser initialization.

Identify what the browser initializes and which parts later HTTP requests need. Depending on the target, that may include scoped cookies, required headers, or other request values. Check pagination separately if the endpoint uses it.

That gives background browser workers a specific job: initialize the target session using an already-inspected collection request. They prepare replacement capacity before it is needed, rather than rendering pages on demand for users.

Prepare the state HTTP will need

A browser context can expose cookies and storage state through Playwright's BrowserContext API. Exporting that state does not turn an HTTP client into a browser: cookies and other browser storage have different roles. See the BrowserContext reference.

For the handoff, select only the cookies and request values required by this target endpoint. Preserve cookie domain, path, expiry, and secure restrictions instead of flattening everything into an unrestricted cookie header. Browser-only state needs separate investigation; a local-storage export is not automatically something the server accepts in an HTTP request.

A bundle carries the state and constraints that the collection worker needs. These are application-level fields, not a curl_cffi export format.

State Example purpose
Allowed origin and path Restrict reuse to the target domain and endpoint
Cookie jar Carry the target session with original cookie scope
Selected headers Preserve the required language or application-specific request value
Response expectations Define the expected content and fields for this target request
Created time and reuse deadline Refresh before the state becomes too old; cookie expiry may shorten this deadline
Generation Distinguish the replacement session from work queued against the previous one
Client and network configuration Keep the intended HTTP profile and, where applicable, the same proxy route

Keep the bundle tied to its endpoint and reuse deadline. Store cookie values securely and keep them out of logs.

Make background preparation and admission explicit

  1. A scheduled or low-watermark refill job opens isolated browser contexts, initializes the target session, and captures only the scoped cookies and necessary request settings.
  2. A background compatibility check creates an owned curl_cffi.requests session with the intended client and network settings. Application code imports compatible cookie state; browser storage is not an automatic export/import bridge.
  3. Fetch the target endpoint over HTTP and compare the returned data and any pagination behavior with the browser baseline. Publish the bundle only after that check succeeds, with its scope, binding, generation, and reuse deadline.
  4. On a user request, exclusively lease compatible ready state to an owned HTTP session. Follow the required pagination and validate each response before returning records. Persist cookie updates with that session, not in a shared jar.
  5. Remove expired or rejected bundles from admission and signal asynchronous replenishment. An empty compatible pool produces an explicit bounded wait or unavailable response; it must not silently start a browser for that user.

curl_cffi documents sessions for cookie persistence and connection reuse in its quick start. Use curl_cffi.requests, its requests-style interface, rather than Python’s separate requests package. Its browser impersonation settings concern network behavior, such as TLS fingerprints; they do not provide a JavaScript runtime or DOM. The HTTP compatibility check must cover the selected profile and proxy route as well as the cookies.

Keep each collection session owned

For the first implementation, let one worker own one HTTP session and process its queue serially. Keep a bundle leased for the whole collection job. Return its updated state to the selectable pool only after the job ends, provided it remains valid; otherwise retire it. Never give the same mutable state to simultaneous jobs.

This keeps session state and cookie updates from leaking between unrelated jobs. Jobs for different domains or incompatible session scopes need separate state. Do not switch a job to an incompatible session just because its original one expired.

Where session acceptance depends on a particular client or proxy route, keep that configuration with the owned session. Changing it calls for another compatibility check.

Refresh without restarting everything

Set a reuse deadline and check it before admitting another request. Refill proactively on a schedule or when ready capacity drops below a chosen threshold. Remove expired or rejected entries, invalidate queued work tied to the old generation, and coalesce replenishment signals under one background owner. User-facing failures must not fan out into browser launches. Bound waiting and retries; if compatible capacity is still unavailable, return that outcome explicitly.

Separate failures by meaning. A transient timeout may allow a bounded retry. Unexpected content or a changed response shape needs inspection. Rate-limit feedback should reduce or pause collection. A login requirement, CAPTCHA, or explicit denial stops this workflow for review.

Where this pattern fits

Use this split when browser setup is occasional and the resulting data requests remain repeatable over HTTP. Keep the browser when each page needs fresh JavaScript execution or state that cannot be transferred reliably. Avoiding repeated rendering is the goal; measure the effect on your workload rather than assuming a particular gain.

Before deployment, exercise the handoff against a representative test fixture, compare accepted records with the browser baseline, and test pool exhaustion, expiry, rejection, and bounded waiting.

Written with AI assistance, based on our team’s scraping experience.

Top comments (0)