1import { ApifyClient } from 'apify-client';
2
3
4
5const client = new ApifyClient({
6 token: '<YOUR_API_TOKEN>',
7});
8
9
10const input = {
11 "runMode": "DEVELOPMENT",
12 "startUrls": [
13 {
14 "url": "https://crawlee.dev/js"
15 }
16 ],
17 "respectRobotsTxtFile": true,
18 "linkSelector": "a[href]",
19 "globs": [
20 {
21 "glob": "https://crawlee.dev/js/*/*"
22 }
23 ],
24 "pseudoUrls": [],
25 "excludes": [
26 {
27 "glob": "/**/*.{png,jpg,jpeg,pdf}"
28 }
29 ],
30 "pageFunction": "// The function accepts a single argument: the \"context\" object.\n// For a complete list of its properties and functions,\n// see https://apify.com/apify/web-scraper#page-function \nasync function pageFunction(context) {\n // This statement works as a breakpoint when you're trying to debug your code. Works only with Run mode: DEVELOPMENT!\n // debugger; \n\n // jQuery is handy for finding DOM elements and extracting data from them.\n // To use it, make sure to enable the \"Inject jQuery\" option.\n const $ = context.jQuery;\n const pageTitle = $('title').first().text();\n const h1 = $('h1').first().text();\n const first_h2 = $('h2').first().text();\n const random_text_from_the_page = $('p').first().text();\n\n\n // Print some information to Actor log\n context.log.info(`URL: ${context.request.url}, TITLE: ${pageTitle}`);\n\n // Manually add a new page to the queue for scraping.\n await context.enqueueRequest({ url: 'http://www.example.com' });\n\n // Return an object with the data extracted from the page.\n // It will be stored to the resulting dataset.\n return {\n url: context.request.url,\n pageTitle,\n h1,\n first_h2,\n random_text_from_the_page\n };\n}",
31 "proxyConfiguration": {
32 "useApifyProxy": true
33 },
34 "initialCookies": [],
35 "waitUntil": [
36 "networkidle2"
37 ],
38 "preNavigationHooks": `// We need to return array of (possibly async) functions here.
39// The functions accept two arguments: the "crawlingContext" object
40// and "gotoOptions".
41[
42 async (crawlingContext, gotoOptions) => {
43 // ...
44 },
45]`,
46 "postNavigationHooks": `// We need to return array of (possibly async) functions here.
47// The functions accept a single argument: the "crawlingContext" object.
48[
49 async (crawlingContext) => {
50 // ...
51 },
52]`,
53 "breakpointLocation": "NONE",
54 "customData": {}
55};
56
57
58const run = await client.actor("morrispetris/web-scraper").call(input);
59
60
61console.log('Results from dataset');
62console.log(`💾 Check your data here: https://console.apify.com/storage/datasets/${run.defaultDatasetId}`);
63const { items } = await client.dataset(run.defaultDatasetId).listItems();
64items.forEach((item) => {
65 console.dir(item);
66});
67
68