-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrawler.js
74 lines (63 loc) · 2.05 KB
/
crawler.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
var request = require('request');
var cheerio = require('cheerio');
var fs = require('fs');
var pageToVisit = "https://www.mohfw.gov.in/";
var path = "data.json";
console.log("Visiting page " + pageToVisit);
/**
* Request Page Link and parse
*/
request(pageToVisit, function (error, response, body) {
if (error) {
console.log("Error: " + error);
}
// Check status code (200 is HTTP OK)
console.log("Status code: " + response.statusCode);
if (response.statusCode === 200) {
// Parse the document body
var $ = cheerio.load(body);
console.log("Page title: " + $('title').text());
const jsonReponse = [];
const columnHeadings = [];
var msg ={};
msg.payload = {
columnHeadings: columnHeadings,
rows: jsonReponse,
}
/**
* Parse table header from page, and store column heading
*/
$('#state-data .table-striped').find('thead').each(function(i, table) {
$(this).find('tr').children().each(function(index, tr){
columnHeadings.push($(this).text().trim());
});
});
/**
* Parse table rows from page, and store rows
*/
$('#state-data .table-striped').find('tbody').each(function(i, table) {
const rowJson = {};
$(this).find('tr').each(function(index, tr){
let row = $(this).text().trim();
rowJson[ index ] = row.split('\n\t');
});
if (JSON.stringify(rowJson) !== '{}') {
jsonReponse.push(rowJson);
}
});
console.log(msg);
/**
* Store parsed data in data.json file
* @param data
* @param /data.json
*/
const storeData = (data, path) => {
try {
fs.writeFileSync(path, "var coronaStatistics=" + JSON.stringify(data))
} catch (err) {
console.error(err)
}
};
storeData(JSON.stringify(msg), path);
}
});