Data extraction in NodeJS

ScrapingBee can extract specific data from a page and return it as structured JSON, so you don’t have to parse the HTML yourself. This kind of JavaScript document extraction normally means fetching the HTML document and querying it with a parser. With ScrapingBee, you send CSS selectors and get back only the values you asked for.

In this tutorial, you’ll use ScrapingBee’s extract_rules parameter to extract the title and description from the ScrapingBee blog with the CSS selectors h1 and p.

Prerequisites

  • Node.js 24 or later
  • A ScrapingBee API key

You can find your API key in your ScrapingBee dashboard after signing in.

Create a project and install the ScrapingBee Node.js SDK:

mkdir node-data-extraction
cd node-data-extraction
npm init -y
npm install scrapingbee@2

Extract Data with extract_rules

Create a file named index.js and add:

const { ScrapingBeeClient } = require("scrapingbee");

async function extractData() {
  const client = new ScrapingBeeClient("YOUR-API-KEY");

  try {
    const response = await client.htmlApi({
      url: "https://www.scrapingbee.com/blog/",
      params: {
        extract_rules: {
          title: "h1",
          description: "p",
        },
      },
    });

    const decoder = new TextDecoder();
    const data = decoder.decode(response.data);
    console.log(data);
  } catch (error) {
    console.error("Error:", error.message);
  }
}

extractData();

Replace YOUR-API-KEY with the API key from your ScrapingBee dashboard.

In the code, title returns the text of the first h1 element, and description returns the text of the first p element on the page. A rule returns only the first match unless you set its type to list.

Then run:

node index.js

Output:

{"title": "The ScrapingBee Blog", "description": "We help you get better at web-scraping: detailed tutorials, case studies and writings by industry experts."}

How extract_rules Works

The extract_rules object maps each output field to a CSS or XPath selector:

extract_rules: {
  title: "h1",
  description: "p",
}

By default, ScrapingBee returns the text content of the selected element. You can also use extraction rules to return HTML, attributes, tables, lists, and nested objects.

You can learn more in ScrapingBee’s Data Extraction documentation. For a CSS selector reference, see W3Schools - CSS Selectors.

Go back to tutorials