1
2
3
4const Apify = require('apify');
5
6Apify.main(async () => {
7
8
9 const input = await Apify.getInput();
10 console.log('Input:');
11 console.dir(input);
12
13
14
15 const metamorphInput = {
16 "breakpointLocation": "NONE",
17 "browserLog": false,
18 "closeCookieModals": false,
19 "debugLog": false,
20 "downloadCss": true,
21 "downloadMedia": true,
22 "excludes": [
23 {
24 "glob": "/**/*.{png,jpg,jpeg,pdf}"
25 }
26 ],
27 "globs": [
28 {
29 "glob": "https://crawlee.dev/*/*"
30 }
31 ],
32 "headless": true,
33 "ignoreCorsAndCsp": false,
34 "ignoreSslErrors": false,
35 "injectJQuery": true,
36 "keepUrlFragments": false,
37 "linkSelector": "a",
38 "pageFunction": "async function pageFunction(context) {\n // request is an instance of Apify.Request (https://sdk.apify.com/docs/api/request)\n // $ is an instance of jQuery (http://jquery.com/)\n const request = context.request;\n const $ = context.jQuery;\n const pageNum = parseInt(request.url.split('?page=').pop());\n\n context.log.info(`Scraping ${context.request.url}`);\n\n // Extract all articles.\n const articles = [];\n $('article').each((index, articleEl) => {\n const $articleEl = $(articleEl);\n\n // H3 contains 2 child elements where first one is topic and second is article title.\n const $h3El = $articleEl.find('h3');\n\n // Extract additonal info and push it to data object.\n articles.push({\n pageNum,\n topic: $h3El.children().first().text(),\n title: $h3El.children().last().text(),\n url: $articleEl.find('a')[0].href,\n teaser: $articleEl.find('.teaser__text').text(),\n });\n });\n\n // Return results.\n return articles;\n}",
39 "postNavigationHooks": `// We need to return array of (possibly async) functions here.
40 // The functions accept a single argument: the "crawlingContext" object.
41 [
42 async (crawlingContext) => {
43 // ...
44 },
45 ]`,
46 "preNavigationHooks": `// We need to return array of (possibly async) functions here.
47 // The functions accept two arguments: the "crawlingContext" object
48 // and "gotoOptions".
49 [
50 async (crawlingContext, gotoOptions) => {
51 // ...
52 },
53 ]`,
54 "proxyConfiguration": {
55 "useApifyProxy": true
56 },
57 "startUrls": [
58 {
59 "url": `https://www.economist.com/${input.category}/?page=1`,
60 "method": "GET"
61 }
62 ],
63 "globs": [
64 {
65 "purl": `https://www.economist.com/${input.category}/?page=[\\d+]`,
66 "method": "GET"
67 }
68 ],
69 "runMode": "DEVELOPMENT",
70
71 "useChrome": false,
72 "waitUntil": [
73 "networkidle2"
74 ]
75 };
76
77
78 await Apify.metamorph('apify/web-scraper', metamorphInput);
79});