Web Scraper avatar
Web Scraper
Try for free

No credit card required

View all Actors
Web Scraper

Web Scraper

apify/web-scraper
Try for free

No credit card required

Crawls arbitrary websites using the Chrome browser and extracts data from pages using a provided JavaScript code. The actor supports both recursive crawling and lists of URLs and automatically manages concurrency for maximum performance. This is Apify's basic tool for web crawling and scraping.

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    "runMode": "DEVELOPMENT",
11    "startUrls": [
12        {
13            "url": "https://crawlee.dev"
14        }
15    ],
16    "linkSelector": "a[href]",
17    "globs": [
18        {
19            "glob": "https://crawlee.dev/*/*"
20        }
21    ],
22    "pseudoUrls": [],
23    "excludes": [
24        {
25            "glob": "/**/*.{png,jpg,jpeg,pdf}"
26        }
27    ],
28    "pageFunction": // The function accepts a single argument: the "context" object.
29    // For a complete list of its properties and functions,
30    // see https://apify.com/apify/web-scraper#page-function 
31    async function pageFunction(context) {
32        // This statement works as a breakpoint when you're trying to debug your code. Works only with Run mode: DEVELOPMENT!
33        // debugger; 
34    
35        // jQuery is handy for finding DOM elements and extracting data from them.
36        // To use it, make sure to enable the "Inject jQuery" option.
37        const $ = context.jQuery;
38        const pageTitle = $('title').first().text();
39        const h1 = $('h1').first().text();
40        const first_h2 = $('h2').first().text();
41        const random_text_from_the_page = $('p').first().text();
42    
43    
44        // Print some information to actor log
45        context.log.info(`URL: ${context.request.url}, TITLE: ${pageTitle}`);
46    
47        // Manually add a new page to the queue for scraping.
48       await context.enqueueRequest({ url: 'http://www.example.com' });
49    
50        // Return an object with the data extracted from the page.
51        // It will be stored to the resulting dataset.
52        return {
53            url: context.request.url,
54            pageTitle,
55            h1,
56            first_h2,
57            random_text_from_the_page
58        };
59    },
60    "proxyConfiguration": {
61        "useApifyProxy": true
62    },
63    "initialCookies": [],
64    "waitUntil": [
65        "networkidle2"
66    ],
67    "preNavigationHooks": `// We need to return array of (possibly async) functions here.
68        // The functions accept two arguments: the "crawlingContext" object
69        // and "gotoOptions".
70        [
71            async (crawlingContext, gotoOptions) => {
72                // ...
73            },
74        ]`,
75    "postNavigationHooks": `// We need to return array of (possibly async) functions here.
76        // The functions accept a single argument: the "crawlingContext" object.
77        [
78            async (crawlingContext) => {
79                // ...
80            },
81        ]`,
82    "breakpointLocation": "NONE",
83    "customData": {}
84};
85
86(async () => {
87    // Run the Actor and wait for it to finish
88    const run = await client.actor("apify/web-scraper").call(input);
89
90    // Fetch and print Actor results from the run's dataset (if any)
91    console.log('Results from dataset');
92    const { items } = await client.dataset(run.defaultDatasetId).listItems();
93    items.forEach((item) => {
94        console.dir(item);
95    });
96})();
Developer
Maintained by Apify
Actor metrics
  • 3.7k monthly users
  • 98.6% runs succeeded
  • 4.9 days response time
  • Created in Mar 2019
  • Modified 3 days ago