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