1import { ApifyClient } from 'apify-client';
2
3
4
5const client = new ApifyClient({
6 token: '<YOUR_API_TOKEN>',
7});
8
9
10const input = {
11 "startUrls": [
12 {
13 "url": "https://www.example.com/"
14 }
15 ],
16 "pageFunction": async function pageFunction(context) {
17 const { request, log, page, $ } = context;
18
19
20
21 const title = page
22 ? await page.title()
23 : $('title').text();
24
25 log.info(`Scraping: ${title}`, { url: request.loadedUrl });
26
27 return {
28 url: request.loadedUrl,
29 title,
30 };
31 },
32 "preNavigationHooks": `// Example: set a cookie before navigation
33// async ({ request, page, session, log }) => {
34// if (page) await page.context().addCookies([{ name: 'auth', value: 'token123', domain: '.example.com' }]);
35// }`,
36 "postNavigationHooks": `// Example: dismiss a popup after page loads
37// async ({ request, page, response, log }) => {
38// if (page) {
39// const popup = page.locator('.popup-close');
40// if (await popup.isVisible()) await popup.click();
41// }
42// }`
43};
44
45
46const run = await client.actor("brilliant_gum/phantom-reborn-crawler").call(input);
47
48
49console.log('Results from dataset');
50console.log(`💾 Check your data here: https://console.apify.com/storage/datasets/${run.defaultDatasetId}`);
51const { items } = await client.dataset(run.defaultDatasetId).listItems();
52items.forEach((item) => {
53 console.dir(item);
54});
55
56