-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
50 lines (43 loc) · 978 Bytes
/
index.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
const axios = require("axios");
const cheerio = require("cheerio");
const fs = require("fs");
(async () => {
try {
const res = await axios.get(
"https://en.wikipedia.org/wiki/List_of_countries_by_median_age"
);
const html = await res.data;
const $ = cheerio.load(html);
const DATA = [];
function extract(elem, index) {
return $($(elem).children()[index])
.text()
.trim();
}
$(".sortable > tbody")
.children()
.each((i, elem) => {
if (i > 0) {
DATA.push({
country: extract(elem, 0),
rank: extract(elem, 1),
median: extract(elem, 2),
male: extract(elem, 3),
female: extract(elem, 4)
});
}
});
await fs.writeFileSync(
"./data.json",
JSON.stringify(
{
data: DATA
},
null,
2
)
);
} catch (err) {
console.error(err);
}
})();