-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
70 lines (65 loc) · 2.12 KB
/
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const puppeteer = require('puppeteer');
const cheerio = require('cheerio');
const { writeFileSync } = require('fs');
const { join } = require('path');
const webPath =
'https://th.wikipedia.org/wiki/%E0%B8%A3%E0%B8%B2%E0%B8%A2%E0%B8%8A%E0%B8%B7%E0%B9%88%E0%B8%AD%E0%B8%AA%E0%B8%96%E0%B8%B2%E0%B8%9A%E0%B8%B1%E0%B8%99%E0%B8%AD%E0%B8%B8%E0%B8%94%E0%B8%A1%E0%B8%A8%E0%B8%B6%E0%B8%81%E0%B8%A9%E0%B8%B2%E0%B9%83%E0%B8%99%E0%B8%9B%E0%B8%A3%E0%B8%B0%E0%B9%80%E0%B8%97%E0%B8%A8%E0%B9%84%E0%B8%97%E0%B8%A2';
function generateRow($, row, tableIndex, limitLength) {
const university = $(row)
.children('td:nth-child(2)')
.text()
.trim();
if (tableIndex !== limitLength) {
const obj = { university };
const code = $(row)
.children('td:nth-child(3)')
.text()
.split('/');
if (code[0]) obj.thCode = code[0].trim();
if (code[1]) obj.enCode = code[1].trim();
return obj;
} else {
return { university };
}
}
function generateTableData(allTables) {
console.log('Generating data..');
return allTables
.map((table, tableIndex) => {
const $ = cheerio.load(table);
return $('tr:nth-child(n+3)')
.map((index, row) =>
generateRow($, row, tableIndex, allTables.length - 1)
)
.get();
})
.reduce((prev, curr) => [...prev, ...curr], []);
}
async function getDataFromBrowser(path) {
console.log('Start fetch data from browser..');
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(path, { waitUntil: 'networkidle2' });
const allTables = await page.$$eval('.wikitable', tables =>
tables.map(t => t.outerHTML.toString())
);
await browser.close();
return allTables;
}
(async () => {
try {
const allTables = await getDataFromBrowser(webPath);
const res = generateTableData(allTables);
writeFileSync(
join(__dirname, './dist/universities-pretty.json'),
JSON.stringify(res, null, 4)
);
writeFileSync(
join(__dirname, './dist/universities.json'),
JSON.stringify(res)
);
console.log('Write file successful');
} catch (e) {
console.error(e);
}
})();