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 "debugLog": false,
19 "downloadCss": true,
20 "downloadMedia": true,
21 "ignoreCorsAndCsp": false,
22 "ignoreSslErrors": false,
23 "injectJQuery": true,
24 "keepUrlFragments": false,
25 "maxRequestRetries": input.maxRequestRetries,
26 "pageFunction":
27
28
29 async function pageFunction(context) {
30
31
32
33
34 const $ = context.jQuery;
35 const search = "#SearchProductCardComponent";
36 let timeoutMillis;
37 await context.waitFor(2000);
38
39
40 const products = document.querySelectorAll(search);
41
42
43 var results = [];
44
45 for (let i = 0; i < products.length-1; i++ )
46 {
47
48 var logoURL = "";
49 var url = "";
50 var name = "";
51 var category = "";
52 try{logoURL = products[i].getElementsByTagName('img')[0].src;}
53 catch(error){}
54 try{url = products[i].childNodes[1].childNodes[0].href;}
55 catch(error){}
56 try{name = products[i].childNodes[1].childNodes[0].text;}
57 catch(error){}
58 try{category = products[i].childNodes[1].childNodes[1].text;}
59 catch(error){}
60 results.push({"name":name,"url":url,"logoURL":logoURL,"category":category})
61
62 }
63
64
65
66
67
68
69
70
71
72 return {
73 results: results
74 };
75 },
76 "postNavigationHooks": `// We need to return array of (possibly async) functions here.
77 // The functions accept a single argument: the "crawlingContext" object.
78 [
79 async (crawlingContext) => {
80 // ...
81 },
82 ]`,
83 "preNavigationHooks": `// We need to return array of (possibly async) functions here.
84 // The functions accept two arguments: the "crawlingContext" object
85 // and "gotoOptions".
86 [
87 async (crawlingContext, gotoOptions) => {
88 // ...
89 },
90 ]`,
91 "proxyConfiguration": {
92 "useApifyProxy": true,
93 "apifyProxyCountry": "US"
94 },
95 "startUrls": input.startUrls,
96 "runMode": "PRODUCTION",
97 "useChrome": false,
98 "waitUntil": [
99 "networkidle2"
100 ]
101 };
102
103
104 await Apify.metamorph('apify/web-scraper', metamorphInput);
105});