JSDOM Scraper avatar

JSDOM Scraper

Try for free

No credit card required

View all Actors
JSDOM Scraper

JSDOM Scraper

apify/jsdom-scraper
Try for free

No credit card required

Parses the HTML using the JSDOM library, providing the same DOM API as browsers do (e.g. `window`). It is able to process client-side JavaScript without using a real browser. Performance-wise, it stands somewhere between the Cheerio Scraper and the browser scrapers.

Do you want to learn more about this Actor?

Get a demo

You can access the JSDOM 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 { window, request, log } = context;
30    
31        // The "window" property contains the JSDOM object which is useful
32        // for querying DOM elements and extracting data from them.
33        const pageTitle = window.document.title;
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/jsdom-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

JSDOM Scraper API in JavaScript

The Apify API client for JavaScript is the official library that allows you to use JSDOM 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:

Developer
Maintained by Apify

Actor Metrics

  • 3 monthly users

  • 4 stars

  • 50% runs succeeded

  • Created in Dec 2022

  • Modified 5 months ago