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

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 dashboard. No credit card required.

Ready to try it?

Run this tutorial on live websites with AlterLab's API. Start free — no credit card required.

Frequently Asked Questions

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 balance. No credit card. No SDK.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 · Up to 5,000 free scrapes · Balance never expire

    How to Scrape Data with Node.js | AlterLab | AlterLab