My Actr Testing avatar
My Actr Testing
Try for free

No credit card required

View all Actors
My Actr Testing

My Actr Testing

xrhibiyftd/my-actr-testing
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 mode

Node.js

Python

curl

1import { ApifyClient } from 'apify-client';
2
3// Initialize the ApifyClient with API token
4const client = new ApifyClient({
5    token: '<YOUR_API_TOKEN>',
6});
7
8// Prepare Actor input
9const input = {
10    "runMode": "PRODUCTION",
11    "startUrls": [
12        {
13            "url": "https://www.example.com"
14        }
15    ],
16    "pageFunction": // The function accepts a single argument: the "context" object.
17    // For a complete list of its properties and functions,
18    // see https://apify.com/apify/web-scraper#page-function 
19    async function pageFunction(context) {
20        // This statement works as a breakpoint when you're trying to debug your code. Works only with Run mode: DEVELOPMENT!
21        // debugger; 
22    
23        // jQuery is handy for finding DOM elements and extracting data from them.
24        // To use it, make sure to enable the "Inject jQuery" option.
25        const $ = context.jQuery;
26        const pageTitle = $('title').first().text();
27        const h1 = $('h1').first().text();
28        const first_h2 = $('h2').first().text();
29        const random_text_from_the_page = $('p').first().text();
30    
31    
32        // Print some information to actor log
33        context.log.info(`URL: ${context.request.url}, TITLE: ${pageTitle}`);
34    
35        // Manually add a new page to the queue for scraping.
36       await context.enqueueRequest({ url: 'http://www.example.com' });
37    
38        // Return an object with the data extracted from the page.
39        // It will be stored to the resulting dataset.
40        return {
41            url: context.request.url,
42            pageTitle,
43            h1,
44            first_h2,
45            random_text_from_the_page
46        };
47    },
48    "proxyConfiguration": {
49        "useApifyProxy": true
50    },
51    "proxyRotation": "UNTIL_FAILURE",
52    "initialCookies": [],
53    "waitUntil": [
54        "networkidle2"
55    ],
56    "preNavigationHooks": `// We need to return array of (possibly async) functions here.
57        // The functions accept two arguments: the "crawlingContext" object
58        // and "gotoOptions".
59        [
60            async (crawlingContext, gotoOptions) => {
61                // ...
62            },
63        ]`,
64    "postNavigationHooks": `// We need to return array of (possibly async) functions here.
65        // The functions accept a single argument: the "crawlingContext" object.
66        [
67            async (crawlingContext) => {
68                // ...
69            },
70        ]`,
71    "breakpointLocation": "NONE",
72    "customData": {}
73};
74
75(async () => {
76    // Run the Actor and wait for it to finish
77    const run = await client.actor("xrhibiyftd/my-actr-testing").call(input);
78
79    // Fetch and print Actor results from the run's dataset (if any)
80    console.log('Results from dataset');
81    const { items } = await client.dataset(run.defaultDatasetId).listItems();
82    items.forEach((item) => {
83        console.dir(item);
84    });
85})();
Developer
Author's avatar
Community logoMaintained by Community
Actor metrics
  • 6 monthly users
  • 100.0% runs succeeded
  • Modified about 1 month ago
Categories

You might also like these Actors