Cheerio Scraper avatar
Cheerio Scraper
Try for free

No credit card required

View all Actors
Cheerio Scraper

Cheerio Scraper

apify/cheerio-scraper
Try for free

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.

The code examples below show how to run the Actor and get its results. To run the code, you need to have an Apify account. Replace <YOUR_API_TOKEN> in the code with your API token, which you can find under Settings > Integrations in Apify Console. Learn mode

Node.js

Python

curl

1import { ApifyClient } from 'apify-client';
2
3// Initialize the ApifyClient with your Apify API token
4const client = new ApifyClient({
5    token: '<YOUR_API_TOKEN>',
6});
7
8// Prepare Actor input
9const input = {
10    "startUrls": [
11        {
12            "url": "https://crawlee.dev"
13        }
14    ],
15    "globs": [
16        {
17            "glob": "https://crawlee.dev/*/*"
18        }
19    ],
20    "pseudoUrls": [],
21    "excludes": [
22        {
23            "glob": "/**/*.{png,jpg,jpeg,pdf}"
24        }
25    ],
26    "linkSelector": "a[href]",
27    "pageFunction": async function pageFunction(context) {
28        const { $, request, log } = context;
29    
30        // The "$" property contains the Cheerio object which is useful
31        // for querying DOM elements and extracting data from them.
32        const pageTitle = $('title').first().text();
33    
34        // The "request" property contains various information about the web page loaded. 
35        const url = request.url;
36        
37        // Use "log" object to print information to actor log.
38        log.info('Page scraped', { url, pageTitle });
39    
40        // Return an object with the data extracted from the page.
41        // It will be stored to the resulting dataset.
42        return {
43            url,
44            pageTitle
45        };
46    },
47    "proxyConfiguration": {
48        "useApifyProxy": true
49    },
50    "initialCookies": [],
51    "additionalMimeTypes": [],
52    "preNavigationHooks": `// We need to return array of (possibly async) functions here.
53        // The functions accept two arguments: the "crawlingContext" object
54        // and "requestAsBrowserOptions" which are passed to the `requestAsBrowser()`
55        // function the crawler calls to navigate..
56        [
57            async (crawlingContext, requestAsBrowserOptions) => {
58                // ...
59            }
60        ]`,
61    "postNavigationHooks": `// We need to return array of (possibly async) functions here.
62        // The functions accept a single argument: the "crawlingContext" object.
63        [
64            async (crawlingContext) => {
65                // ...
66            },
67        ]`,
68    "customData": {}
69};
70
71(async () => {
72    // Run the Actor and wait for it to finish
73    const run = await client.actor("apify/cheerio-scraper").call(input);
74
75    // Fetch and print Actor results from the run's dataset (if any)
76    console.log('Results from dataset');
77    const { items } = await client.dataset(run.defaultDatasetId).listItems();
78    items.forEach((item) => {
79        console.dir(item);
80    });
81})();
Developer
Maintained by Apify
Actor metrics
  • 426 monthly users
  • 99.7% runs succeeded
  • 4.5 days response time
  • Created in Apr 2019
  • Modified 7 days ago