te2efwdfg avatar
te2efwdfg
Try for free

No credit card required

View all Actors
te2efwdfg

te2efwdfg

astounding_lever/wb-spr-eg3g
Try for free

No credit card required

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 more

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("astounding_lever/wb-spr-eg3g").call(input);
89
90    // Fetch and print Actor results from the run's dataset (if any)
91    console.log('Results from dataset');
92    console.log(`💾 Check your data here: https://console.apify.com/storage/datasets/${run.defaultDatasetId}`);
93    const { items } = await client.dataset(run.defaultDatasetId).listItems();
94    items.forEach((item) => {
95        console.dir(item);
96    });
97})();
98
99// 📚 Want to learn more 📖? Go to → https://docs.apify.com/api/client/js/docs
Developer
Maintained by Community
Actor metrics
  • 1 monthly user
  • 0 stars
  • 100.0% runs succeeded
  • Created in Jan 2024
  • Modified 6 months ago
Categories