Cheerio Scraper
No credit card required
Cheerio Scraper
No credit card required
Crawls websites using raw HTTP requests, parses the HTML with the Cheerio library, and extracts data from the pages using a Node.js code. Supports both recursive crawling and lists of URLs. This actor is a high-performance alternative to apify/web-scraper for websites that do not require JavaScript.
Do you want to learn more about this Actor?
Get a demoYou can access the Cheerio Scraper programmatically from your own JavaScript applications by using the Apify API. You can also choose the language preference from below. To use the Apify API, you’ll need an Apify account and your API token, found in Integrations settings in Apify Console.
1import { ApifyClient } from 'apify-client';
2
3// Initialize the ApifyClient with your Apify API token
4// Replace the '<YOUR_API_TOKEN>' with your token
5const client = new ApifyClient({
6 token: '<YOUR_API_TOKEN>',
7});
8
9// Prepare Actor input
10const input = {
11 "startUrls": [
12 {
13 "url": "https://crawlee.dev"
14 }
15 ],
16 "globs": [
17 {
18 "glob": "https://crawlee.dev/*/*"
19 }
20 ],
21 "pseudoUrls": [],
22 "excludes": [
23 {
24 "glob": "/**/*.{png,jpg,jpeg,pdf}"
25 }
26 ],
27 "linkSelector": "a[href]",
28 "pageFunction": async function pageFunction(context) {
29 const { $, request, log } = context;
30
31 // The "$" property contains the Cheerio object which is useful
32 // for querying DOM elements and extracting data from them.
33 const pageTitle = $('title').first().text();
34
35 // The "request" property contains various information about the web page loaded.
36 const url = request.url;
37
38 // Use "log" object to print information to actor log.
39 log.info('Page scraped', { url, pageTitle });
40
41 // Return an object with the data extracted from the page.
42 // It will be stored to the resulting dataset.
43 return {
44 url,
45 pageTitle
46 };
47 },
48 "proxyConfiguration": {
49 "useApifyProxy": true
50 },
51 "initialCookies": [],
52 "additionalMimeTypes": [],
53 "preNavigationHooks": `// We need to return array of (possibly async) functions here.
54 // The functions accept two arguments: the "crawlingContext" object
55 // and "requestAsBrowserOptions" which are passed to the `requestAsBrowser()`
56 // function the crawler calls to navigate..
57 [
58 async (crawlingContext, requestAsBrowserOptions) => {
59 // ...
60 }
61 ]`,
62 "postNavigationHooks": `// We need to return array of (possibly async) functions here.
63 // The functions accept a single argument: the "crawlingContext" object.
64 [
65 async (crawlingContext) => {
66 // ...
67 },
68 ]`,
69 "customData": {}
70};
71
72// Run the Actor and wait for it to finish
73const run = await client.actor("apify/cheerio-scraper").call(input);
74
75// Fetch and print Actor results from the run's dataset (if any)
76console.log('Results from dataset');
77console.log(`💾 Check your data here: https://console.apify.com/storage/datasets/${run.defaultDatasetId}`);
78const { items } = await client.dataset(run.defaultDatasetId).listItems();
79items.forEach((item) => {
80 console.dir(item);
81});
82
83// 📚 Want to learn more 📖? Go to → https://docs.apify.com/api/client/js/docs
Cheerio Scraper - HTML scraping tool API in JavaScript
The Apify API client for JavaScript is the official library that allows you to use Cheerio Scraper API in JavaScript or TypeScript, providing convenience functions and automatic retries on errors.
Install the apify-client
npm install apify-client
Other API clients include:
Actor Metrics
440 monthly users
-
84 stars
>99% runs succeeded
Created in Apr 2019
Modified a month ago