Skip to content

Instantly share code, notes, and snippets.

@DZuz14
Created September 27, 2021 15:38
Show Gist options
  • Save DZuz14/5eea85506dfa1a50cdaeaab670a860a4 to your computer and use it in GitHub Desktop.
Save DZuz14/5eea85506dfa1a50cdaeaab670a860a4 to your computer and use it in GitHub Desktop.
Corona 5
const puppeteer = require("puppeteer");
const createCsvWriter = require("csv-writer").createArrayCsvWriter;
const csvWriter = createCsvWriter({
header: [
"Country",
"Total Cases",
"New Cases",
"Total Deaths",
"New Deaths",
"Total Recovered",
"New Recovered",
"Active Cases",
"Serious/Critical",
"Tot Cases/1M pop",
"Total Tests",
"Tests/1M pop",
"Population",
],
path: "./coronavirus.csv",
});
(async () => {
try {
const browser = await puppeteer.launch({ headless: false });
const page = await browser.newPage();
await page.goto("https://www.worldometers.info/coronavirus/");
await page.waitForSelector("table#main_table_countries_today");
const worldData = await page.evaluate(() => {
const tbody = document.querySelector(
"table#main_table_countries_today tbody"
);
const trs = Array.from(
tbody.querySelectorAll("tr:not(.total_row_world)")
);
const worldData = [];
for (const tr of trs) {
const tds = Array.from(tr.querySelectorAll("td"));
const data = tds.slice(1, 15).map((td) => td.innerText);
worldData.push(data);
}
return worldData;
});
await csvWriter.writeRecords(worldData);
await browser.close();
} catch (error) {
console.log(error);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment