This repository has been archived by the owner on Mar 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
84 lines (69 loc) · 2.23 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const express = require('express');
const dotenv = require('dotenv');
const fs = require("fs");
var stringify = require("csv-stringify");
const puppeteer = require("puppeteer");
dotenv.config({path:'./config/config.env'})
const PORT = process.env.PORT;
const app = express();
uri =
"https://www.arcgis.com/home/item.html?id=38d6e70833984ab882ef04deb96c36f0&sublayer=0#data";
fetchData = async () => {
//using puppteer
const browser = await puppeteer.launch({
args: ["--no-sandbox"],
// product:'firefox',
//executablePath: path.resolve(
// __dirname,
// "../node_modules/puppeteer/.local-firefox/linux-79.0a1/firefox/firefox"
// ),
headless: true,
});
const page = await browser.newPage();
const navigationPromise = page.waitForNavigation();
await page.goto(uri);
//await scrollToBottom(page);
await page.setViewport({
width: 800,
height: 600,
});
// await autoScroll(page);
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
await sleep(10000);
const dataHeader = await page.evaluate(() => {
const tds = Array.from(document.querySelectorAll("table tr th"));
return tds.map((td) => td.innerText);
});
const data = await page.evaluate(() => {
//const headers = Array.from( document.querySelectorAll("table th "));
const rows = document.querySelectorAll("table tr ");
return Array.from(rows, (row) => {
const columns = row.querySelectorAll("td");
return Array.from(columns, (column) => column.innerText);
});
//const tds = Array.from(document.querySelectorAll("table tr td"));
//return tds.map((td) => td.innerText);
});
console.log("cases: ", data);
console.log("cases header: ", dataHeader);
stringify(
{ header: true, columns: dataHeader },
data,
(err, output) => {
fs.writeFile("input.csv", output, "utf8", function (err) {
if (err) {
console.log(
"Some error occured - file either not saved or corrupted file saved."
);
} else {
console.log("It's saved!");
}
});
}
);
await navigationPromise
await page.close();
await browser.close();
};
fetchData();
app.listen(PORT, console.log(`Server listening on port ${PORT}`));