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://crawlee.dev/js"
14 }
15 ],
16 "respectRobotsTxtFile": true,
17 "globs": [
18 {
19 "glob": "https://crawlee.dev/js/*/*"
20 }
21 ],
22 "pseudoUrls": [],
23 "excludes": [
24 {
25 "glob": "/**/*.{png,jpg,jpeg,pdf}"
26 }
27 ],
28 "linkSelector": "a[href]",
29 "pageFunction": async function pageFunction(context) {
30 const { $, request, log } = context;
31
32
33
34 const pageTitle = $('title').first().text();
35
36
37 const url = request.url;
38
39
40 log.info('Page scraped', { url, pageTitle });
41
42
43
44 return {
45 url,
46 pageTitle
47 };
48 },
49 "proxyConfiguration": {
50 "useApifyProxy": true
51 },
52 "initialCookies": [],
53 "additionalMimeTypes": [],
54 "preNavigationHooks": "// We need to return array of (possibly async) functions here.\n// The functions accept two arguments: the \"crawlingContext\" object\n// and \"requestAsBrowserOptions\" which are passed to the `requestAsBrowser()`\n// function the crawler calls to navigate..\n[\n async (crawlingContext, requestAsBrowserOptions) => {\n // ...\n }\n]",
55 "postNavigationHooks": `// We need to return array of (possibly async) functions here.
56// The functions accept a single argument: the "crawlingContext" object.
57[
58 async (crawlingContext) => {
59 // ...
60 },
61]`,
62 "customData": {}
63};
64
65
66const run = await client.actor("apify/cheerio-scraper").call(input);
67
68
69console.log('Results from dataset');
70console.log(`💾 Check your data here: https://console.apify.com/storage/datasets/${run.defaultDatasetId}`);
71const { items } = await client.dataset(run.defaultDatasetId).listItems();
72items.forEach((item) => {
73 console.dir(item);
74});
75
76