1
2
3
4
5
6const Apify = require('apify');
7
8Apify.main(async () => {
9
10
11
12
13 console.log('URL:');
14 const input = {url: "https://covid19.who.int/"};
15 if (!input || !input.url) throw new Error('Input must be a JSON object with the "url" field!');
16
17 console.log('Launching Puppeteer...');
18 const browser = await Apify.launchPuppeteer();
19
20 console.log(`Opening page ${input.url}...`);
21 const page = await browser.newPage();
22 await page.goto(input.url);
23 await page.waitForXPath(`//*[@id="gatsby-focus-wrapper"]/div/div[4]/div[1]/div/div/div[2]/div[3]/span[1]`, {visible: true});
24 const [casesElem] = await page.$x(`//*[@id="gatsby-focus-wrapper"]/div/div[4]/div[1]/div/div/div[2]/div[3]/span[1]`);
25 const casesCommas = await page.evaluate(name => name.innerText, casesElem);
26 const cases = parseInt(casesCommas.replaceAll(",",""));
27
28 await page.waitForXPath(`//*[@id="gatsby-focus-wrapper"]/div/div[4]/div[1]/div/div/div[2]/div[4]/span[1]`, {visible:true});
29 const [deathsElem] = await page.$x(`//*[@id="gatsby-focus-wrapper"]/div/div[4]/div[1]/div/div/div[2]/div[4]/span[1]`);
30 const deathsCommas = await page.evaluate(name => name.innerText, deathsElem);
31 const deaths = parseInt(deathsCommas.replaceAll(",",""));
32
33 console.log('Saving output...');
34 await Apify.setValue('OUTPUT', {
35 cases,
36 casesCommas,
37 deaths,
38 deathsCommas,
39 });
40
41 console.log('Closing Puppeteer...');
42 await browser.close();
43
44 console.log('Done.');
45});