AI agents & automation

Web Scraping With Playwright for Business Intelligence

A practical engineering guide to building Playwright scrapers that keep working on messy, dynamic websites, and turning their output into business intelligence you can act on.

By Devang GandhiUpdated Nov 3, 20267 min read

Most web scraping Playwright tutorials stop at grabbing a page title. In real projects, Playwright is my default when business data lives on websites that were never designed to be read by machines: government portals, listing sites, supplier catalogs, competitor pages. These sites often depend on JavaScript, session cookies and multi-step forms, which break simple HTTP scrapers.

I used Playwright to build a government tender intelligence bot as part of the Antoc product work, scraping .gov.in portals on a GCP VM under Xvfb and classifying results with Gemini. This post covers the engineering patterns that keep a scraper running unattended for months, not just in a demo.

TL;DR

Use Playwright when you need a real browser; otherwise fetch HTML or call the underlying API. Write selectors against stable attributes, rely on Playwright's auto-waiting and explicit waits for data, and parse defensively. Run on a VM with a virtual display if headless mode misbehaves, store raw and parsed data separately, deduplicate, and alert when a run finds nothing or fails.

key takeaways

  • Use web scraping with Playwright when pages need JavaScript, sessions or real interactions; use plain HTTP requests when the data is in the HTML or an API.
  • Stable selectors, explicit waits and defensive parsing matter more than speed for scrapers that must run unattended.
  • Running a headed browser under Xvfb on a cloud VM helps with sites that behave differently in headless mode.
  • Store raw snapshots and parsed records separately, deduplicate by a natural key and alert on failures.

When a browser is actually needed

A browser is the most expensive way to fetch a page. Before writing Playwright code, I check the network tab.

What you see Best approach
Data is in the initial HTML HTTP request plus an HTML parser
Page calls a JSON API you can reproduce Call the API directly
Content rendered by JavaScript, no clean API Playwright
Multi-step forms, filters, session state Playwright
Downloads behind clicks or generated links Playwright with download handling

Many government and enterprise portals fall into the last three rows: search forms with dropdowns that load dynamically, session tokens in hidden fields and results that only appear after a click. That is where Playwright earns its cost.

Writing scrapers that do not break weekly

Locators

Playwright's locator guide recommends user-facing locators, and that advice applies to scraping too. In order of preference:

  1. Role and accessible name, like a button named "Search".
  2. Stable attributes such as id, name or data-*.
  3. Text content for labels that rarely change.
  4. CSS structure only as a last resort, scoped as tightly as possible.

Long CSS paths like div > div:nth-child(3) > table tr td break the first time the site adds a banner.

Waiting

Playwright auto-waits for elements to be actionable, but data loading is a different problem. After submitting a search, wait for the results table or for a "no results" message, whichever comes first. Avoid fixed sleeps; they are either too long, which wastes time, or too short, which causes random failures.

Pagination and lists

Loop through pages by clicking "next" until it is disabled or absent, with a hard maximum to prevent infinite loops. Extract rows into plain objects on each page before moving on, so a failure on page 12 does not lose pages 1 to 11.

Defensive parsing

Real-world pages are inconsistent. Dates come in multiple formats, values have stray whitespace, some rows have missing cells. Parse each field with a small function that returns null on failure and logs the raw value, instead of throwing and killing the run.

Running scrapers in production

A scraper that runs on your laptop is a prototype. Production needs a home, a schedule and monitoring.

  • Host: a small cloud VM is often simpler than serverless for browser workloads, because browsers need memory, disk for downloads and longer run times.
  • Headed under Xvfb: some sites behave differently in headless mode. Running Chromium headed inside an Xvfb virtual display on a Linux VM avoids those differences without needing a real screen.
  • Process management: run on a schedule with cron or a process manager, and make sure the browser is always closed, even on errors.
  • Politeness: low concurrency, small delays between requests, and only the pages you need.
  • Alerting: alert not just on crashes but on suspicious success, such as zero results from a source that usually returns dozens. That usually means the site changed.

Turning scraped data into intelligence

Raw scraped rows are not intelligence. The value comes from what happens after.

  1. Store raw snapshots (HTML or extracted text) so you can re-parse when your parser improves.
  2. Normalize into a clean schema with consistent dates, amounts and categories.
  3. Deduplicate by a natural key such as a reference number plus source, so re-runs do not create duplicates.
  4. Classify or enrich, often with an LLM, to decide which records matter to the business.
  5. Deliver to where people work: a dashboard, a CRM record or a message.

How I built this in practice

The tender intelligence bot scrapes .gov.in tender portals with Playwright running on a GCP VM with Xvfb, because headed Chromium proved more reliable on those portals than headless mode. Scraped tenders are stored in MongoDB, deduplicated, and then a Gemini classifier decides which tenders are relevant to the business before anyone sees them.

The same infrastructure habits from the Antoc platform apply: GCP VMs, process management with PM2, and clear separation between collecting data and processing it. The follow-up post on tender scraping goes deeper into the classifier and delivery side. See the Antoc case study for the wider platform, or my UK hiring page if you need a developer for a data project from the UK.

Common mistakes

  • Reaching for a browser when a simple HTTP request or the site's own API would do.
  • Brittle CSS paths and fixed sleeps that make the scraper flaky.
  • Throwing on the first malformed field and losing the whole run.
  • Running only on a laptop with no schedule, logs or alerts.
  • Treating zero results as success instead of a likely breakage.
  • Storing only parsed data, so a parser bug means re-scraping everything.

keep reading

FAQ

Is Playwright better than Puppeteer or Selenium for scraping?+

For most new projects I prefer Playwright because of its auto-waiting, reliable locators, multi-browser support and good tooling for debugging. Puppeteer and Selenium can do the job, but Playwright usually means less flaky code.

Is web scraping legal?+

It depends on the site, the data and your jurisdiction. Public, non-personal data is generally lower risk, but you should respect terms of service and robots.txt, avoid personal data, keep request rates low and get legal advice for anything sensitive.

Why does my scraper work locally but fail on a server?+

Common causes are missing browser dependencies, sites behaving differently for headless browsers, different time zones or locales, and IP-based restrictions. Running a headed browser under a virtual display like Xvfb and matching locale settings often fixes it.

How often should a business scraper run?+

As often as the data actually changes and the business needs it, and no more. Daily or a few times a day is typical for listings and notices. Running more often adds load on the source site without adding value.

Devang Gandhi

AI & full stack developer at Passdn, building CRMs, SaaS, AI agents and Flutter apps for clients in the US, UK, Australia, UAE and India. More about me

Read next

More in AI agents & automation