Web scraping with JavaScript: Node.js can do it without a browser

JavaScript people reach for Puppeteer first, because it is the browser they know. For most pages a plain fetch in Node.js gets the same HTML in a fraction of the time. Start there, and keep the browser for the pages that need it.

Web scraping with JavaScript

import * as cheerio from "cheerio";
const html = await (await fetch(url)).text();
const $ = cheerio.load(html);
console.log($("h1").text().trim());

Nederlands

Web scraping in Node.js, step by step

  1. Fetch the page. Node 18 and newer have fetch built in. No axios needed.

  2. Check the source, not DevTools. The Elements tab shows the page after JavaScript ran. View source shows what fetch gets. Search there for your value.

  3. Pick the value. cheerio gives you jQuery-style selectors on the server. For JSON inside the page (JSON-LD, __NEXT_DATA__), JSON.parse beats any selector.

  4. Limit how many run at once. Promise.all on ten thousand links is a small attack. Run a few at a time, with a pause.

cheerio, Puppeteer or Playwright?

  1. cheerio. Parses HTML and runs no scripts. Milliseconds a page.

  2. Puppeteer and Playwright. Drive a real Chrome. For pages built in the browser, and for clicks. Seconds and hundreds of MB a page.

  3. In the browser itself. A snippet in the console or a userscript is fine for the one page you are looking at, not for a crawl.

Scraping JavaScript-rendered pages without a browser

A page that renders in the browser still gets its data from somewhere. Often it sits in the HTML as a JSON blob, or the page calls a JSON URL you can see in the network tab (check the terms before you use it). Either is faster and steadier than waiting for a headless browser to paint.

A free JavaScript script with nothing to install

The script generator writes one extract.mjs for Node 18 or newer, with no packages: a rule per field, backups for each, and for every value how sure it is. Import extract(html) into your own code, or run the file on a list of links or a sitemap.

More on this

Tested on 207 real pages from 49 sites: 609 of 659 values exactly right, 0 wrong. The rest came back as not found, unsure or in another format.

Made by Simon. I have been scraping for more than five years and building software since 2015.

Try it on your own pages

Paste a few links and a couple of values. The look is free, and you hear the price before anything is due.

Try it on your pages

The first look is free, and you hear the price before anything is due. If I can't get your examples right, you don't pay.