![My Actr Testing avatar](https://images.apifyusercontent.com/-mdiYg4bhiPWk7kMZ70umV8IHzEGEuuLlcsM1fm6zz4/rs:fill:250:250/aHR0cHM6Ly9hcGlmeS1pbWFnZS11cGxvYWRzLXByb2QuczMuYW1hem9uYXdzLmNvbS9ZSkNuUzlxb2dpOVh4RGdMQi9YeWd0Snl4RVFpWG4zV2Vwcy1JTUdfNzYzNXNhLTA4LnBuZw.webp)
My Actr Testing
Try for free
No credit card required
View all Actors
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": "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 console.log(`💾 Check your data here: https://console.apify.com/storage/datasets/${run.defaultDatasetId}`);
82 const { items } = await client.dataset(run.defaultDatasetId).listItems();
83 items.forEach((item) => {
84 console.dir(item);
85 });
86})();
87
88// 📚 Want to learn more 📖? Go to → https://docs.apify.com/api/client/js/docs
Actor metrics
- 6 monthly users
- 1 star
- 100.0% runs succeeded
- Created in Jan 2024
- Modified 8 months ago
Categories