Skip to content
TodayTell Data

Scrape a category page with Playwright and export to CSV

Updated 2026-11-25 · 5 min read · By BYM Data team

Playwright gives you a real browser, so a JS-rendered listing becomes ordinary DOM work. The script below loads a category page, waits for the product grid, paginates, and writes CSV — with a delay between requests and no attempt to bypass a block.

Install

pip install playwright
playwright install chromium

The script

import csv, time
from playwright.sync_api import sync_playwright

def scrape(url, pages=3):
    rows = []
    with sync_playwright() as p:
        browser = p.chromium.launch()
        page = browser.new_page(user_agent="Mozilla/5.0 (research; contact you@company.com)")
        for n in range(1, pages + 1):
            page.goto(f"{url}?page={n}", wait_until="domcontentloaded")
            page.wait_for_selector("[data-product]", timeout=15000)
            for card in page.query_selector_all("[data-product]"):
                rows.append({
                    "title": (card.query_selector("h3") or card).inner_text().strip(),
                    "price": (card.query_selector("[data-price]") or card).inner_text().strip(),
                    "url": card.query_selector("a").get_attribute("href"),
                })
            time.sleep(2)  # be a good citizen
        browser.close()
    return rows

rows = scrape("https://example.com/category")
with open("products.csv", "w", newline="", encoding="utf-8") as f:
    w = csv.DictWriter(f, fieldnames=rows[0].keys()); w.writeheader(); w.writerows(rows)
print(len(rows), "rows")

What breaks at scale

  • Selectors drift when the site changes its markup
  • Rendering 50,000 pages costs real money and time
  • Blocks appear past a few hundred requests from one IP
  • Duplicates across pages need a stable key, not the title

Want this data without building it?

Free 100-row sample, written quote in 24 hours.

Get a quote

When to stop hand-rolling

If you are adding proxies, retries, a scheduler and a diff job, you are building the thing we run. That is the point where a written quote is cheaper than another sprint.

Questions

Is Playwright allowed on any site?

No — check the terms and robots first. This tutorial is for sites you are permitted to read.

Why the delay?

Politeness and stability. Hammering a server gets you blocked, which is a worse outcome than being slow.

Can I use this for 100,000 pages?

You can, but rendering cost and drift make it a bad use of a laptop. Use a queue and a scheduler, or buy the delivery.

BYM Data team

Data engineering — We build and run the extraction pipelines behind every dataset we sell. Guides are written by whoever maintains the source, and reviewed by a second engineer before publishing.

Get a free sampleWhatsApp