Beginner4 steps

How to Scrape Data with Node.js

Node.js is an excellent platform for web scraping pipelines — native async/await makes concurrent requests natural, and the npm ecosystem provides powerful HTML parsing tools. This guide covers the complete setup using modern Node.js.

Step-by-Step Guide

1

Install Cheerio for HTML parsing

Run `npm install cheerio` to install Cheerio — a server-side jQuery implementation for parsing HTML. No headless browser needed — AlterLab handles rendering.

2

Fetch a page using the AlterLab API

Use Node.js native fetch (Node 18+) or axios to POST to the AlterLab API. Pass your URL and API key in the request body.

3

Parse with Cheerio

Load the returned HTML into Cheerio with `cheerio.load(html)`. Use jQuery-style selectors to extract the data you need.

4

Process and save results

Write results to a JSON file with `fs.writeFileSync`, send to a database, or pipe to further processing steps.

Code Example

TypeScript
import * as cheerio from "cheerio";
import fs from "fs";

const API_KEY = "YOUR_API_KEY";

async function scrapePage(url: string) {
  const response = await fetch("https://alterlab.io/api/v1/scrape", {
    method: "POST",
    headers: {
      "X-API-Key": API_KEY,
      "Content-Type": "application/json",
    },
    body: JSON.stringify({ url, render_js: true }),
  });

  const data = await response.json();
  const $ = cheerio.load(data.html);

  const results: string[] = [];
  $("h2.article-title").each((_i, el) => {
    results.push($(el).text().trim());
  });

  fs.writeFileSync("output.json", JSON.stringify(results, null, 2));
  console.log(`Extracted ${results.length} items`);
}

scrapePage("https://example.com/articles");

Replace YOUR_API_KEY with your key from the . No credit card required.

Try this yourself with AlterLab

Run this tutorial on live websites with AlterLab's API. Free tier includes 5,000 requests — no credit card required.

View API docs

Frequently Asked Questions

Can I use Puppeteer or Playwright instead of Cheerio?

You can, but it's unnecessary when using AlterLab — the API already handles browser rendering on the server side. Using Cheerio to parse the returned HTML is simpler, faster, and uses fewer resources than running a local browser.

How do I scrape multiple URLs concurrently in Node.js?

Use `Promise.all()` with an array of fetch calls to send multiple requests concurrently. Add a concurrency limiter like the `p-limit` package to avoid overwhelming the target server.

Responsible Use

AlterLab is designed for extracting publicly available data. Always review the terms of service for any website you access, respect robots.txt directives, and ensure your use case complies with applicable laws in your jurisdiction.

More tutorials

Browse all how-to guides for web scraping — from beginner extractions to advanced multi-page pipelines.

Your first scrape.
Sixty seconds.

$1 free credit — up to 5,000 scrapes. No credit card.Just a POST request.

terminal
curl -X POST https://api.alterlab.io/v1/scrape \
-H "X-API-Key: YOUR_KEY" \
-H "Content-Type: application/json" \
-d '{"url": "https://example.com", "formats": ["markdown"]}'

No credit card required · $1 free credit, up to 5,000 scrapes · Balance never expires