DEV Community

TateLyman
TateLyman

Posted on

How to Build a Web Scraper with Playwright in 20 Lines of Code

Playwright makes web scraping trivially easy. Here's a complete scraper in 20 lines:

const { chromium } = require("playwright");

async function scrape(url) {
  const browser = await chromium.launch();
  const page = await browser.newPage();
  await page.goto(url);

  const title = await page.title();
  const text = await page.textContent("body");
  const links = await page.$$eval("a[href]", 
    els => els.map(a => a.href).filter(h => h.startsWith("http"))
  );
  const images = await page.$$eval("img[src]",
    els => els.map(img => img.src)
  );

  await browser.close();
  return { title, text: text.slice(0, 1000), links, images };
}

scrape("https://example.com").then(console.log);
Enter fullscreen mode Exit fullscreen mode

This handles:

  • JavaScript-rendered pages (React, Vue, Angular)
  • Auto-waiting for elements
  • All modern browsers (Chrome, Firefox, Safari)
  • Headless or headed mode

Want it as an API?

I built a free scraping API: GET devtools-site-delta.vercel.app/api/scrape?url=...

Returns title, description, text, links, images, headings, meta tags as JSON.

Need a custom scraper?

I build custom scrapers for $50-$100. Scheduled runs, alerts, data export.

DM me or email lymantate2@gmail.com

Top comments (0)