1import { ApifyClient } from 'apify-client';
2
3
4
5const client = new ApifyClient({
6 token: '<YOUR_API_TOKEN>',
7});
8
9
10const input = {
11 "requests": [
12 {
13 "url": "https://apify.com"
14 }
15 ],
16 "pseudoUrls": [
17 {
18 "purl": "https://apify.com[(/[\\w-]+)?]"
19 }
20 ],
21 "linkSelector": "a[href]",
22 "pageFunction": async function pageFunction(context) {
23 const { window, document, crawler, enqueueRequest, request, response, userData, json, body, kvStore, customData } = context;
24
25 const title = document.querySelector('title').textContent
26
27 const responseHeaders = response.headers
28
29 return {
30 title,
31 responseHeaders
32 };
33 },
34 "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]",
35 "postNavigationHooks": `// We need to return array of (possibly async) functions here.
36// The functions accept a single argument: the "crawlingContext" object.
37[
38 async (crawlingContext) => {
39 // ...
40 },
41]`,
42 "proxy": {
43 "useApifyProxy": true
44 },
45 "additionalMimeTypes": [],
46 "customData": {}
47};
48
49
50const run = await client.actor("mstephen190/vanilla-js-scraper").call(input);
51
52
53console.log('Results from dataset');
54console.log(`💾 Check your data here: https://console.apify.com/storage/datasets/${run.defaultDatasetId}`);
55const { items } = await client.dataset(run.defaultDatasetId).listItems();
56items.forEach((item) => {
57 console.dir(item);
58});
59
60