TypeScript one-page scraper
Get data from one web page with Cheerio. The simplest way to start scraping.
src/main.ts
1// Apify SDK - toolkit for building Apify Actors (Read more at https://docs.apify.com/sdk/js/).2import { Actor, log } from 'apify';3// Axios - Promise based HTTP client for the browser and node.js (Read more at https://axios-http.com/docs/intro).4import axios from 'axios';5// Cheerio - The fast, flexible & elegant library for parsing and manipulating HTML and XML (Read more at https://cheerio.js.org/).6import * as cheerio from 'cheerio';7
8// this is ESM project, and as such, it requires you to specify extensions in your relative imports9// read more about this here: https://nodejs.org/docs/latest-v18.x/api/esm.html#mandatory-file-extensions10// note that we need to use `.js` even when inside TS files11// import { router } from './routes.js';12
13// The init() call configures the Actor to correctly work with the Apify-provided environment - mainly the storage infrastructure. It is necessary that every Actor performs an init() call.14await Actor.init();15
16interface Input {17 url: string;18}19// Structure of input is defined in input_schema.json20const input = await Actor.getInput<Input>();21if (!input) throw new Error('Input is missing!');22const { url } = input;23
24// Fetch the HTML content of the page.25const response = await axios.get(url);26
27// Parse the downloaded HTML with Cheerio to enable data extraction.28const $ = cheerio.load(response.data);29
30// Extract all headings from the page (tag name and text).31const headings: { level: string; text: string }[] = [];32$('h1, h2, h3, h4, h5, h6').each((_i, element) => {33 const headingObject = {34 level: $(element).prop('tagName')!.toLowerCase(),35 text: $(element).text(),36 };37 log.info('Extracted heading', headingObject);38 headings.push(headingObject);39});40
41// Save headings to Dataset - a table-like storage.42await Actor.pushData(headings);43
44// Gracefully exit the Actor process. It's recommended to quit all Actors with an exit().45await Actor.exit();A template for scraping data from a single web page in TypeScript (Node.js). The URL of the web page is passed in via input, which is defined by the input schema . The template uses the Axios client to get the HTML of the page and the Cheerio library to parse the data from it. The data are then stored in a dataset where you can easily access them.
The scraped data in this template are page headings but you can easily edit the code to scrape whatever you want from the page.
- Apify SDK - a toolkit for building Actors
- Input schema - define and easily validate a schema for your Actor's input
- Dataset - store structured data where each object stored has the same attributes
- Axios client - promise-based HTTP Client for Node.js and the browser
- Cheerio - library for parsing and manipulating HTML and XML
-
Actor.getInput()gets the input where the page URL is defined -
axios.get(url)fetches the page -
cheerio.load(response.data)loads the page data and enables parsing the headings -
This parses the headings from the page and here you can edit the code to parse whatever you need from the page
$("h1, h2, h3, h4, h5, h6").each((_i, element) => {...}); -
Actor.pushData(headings)stores the headings in the dataset
- Web scraping in Node.js with Axios and Cheerio
- Web scraping with Cheerio in 2023
- Video tutorial on building a scraper using CheerioCrawler
- Written tutorial on building a scraper using CheerioCrawler
- Integration with Zapier , Make, Google Drive, and others
- Video guide on getting scraped data using Apify API
- A short guide on how to build web scrapers using code templates:
TypeScript Cheerio crawler
A fast HTTP crawler that extracts data from every page. Good for simple sites like blogs, news, or product listings, but it can't run client-side JavaScript.
TypeScript Puppeteer scraper
A headless Chrome scraper that renders JavaScript before extracting data. Good for social feeds, dashboards, or single-page apps.
TypeScript Playwright scraper
A Playwright-based browser scraper supporting multiple browsers and contexts.
TypeScript Camoufox scraper
A Firefox-based browser built to look like a real user and bypass bot protection.
Playwright test runner
An automated browser test runner with results you can access via API.
Empty TypeScript Actor
An Actor with the Apify SDK set up, so you can build any tool you need.