-
Notifications
You must be signed in to change notification settings - Fork 0
/
crawler.js
117 lines (95 loc) · 3.5 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
let request = require('request');
let cheerio = require('cheerio');
let fs = require('fs')
/**
* Get all filenames from the Funda directory that end with *.html
*/
let files = fs.readdirSync('./Funda');
let fileNames = [];
for(let i = 0; i < files.length; i++) {
if(files[i].endsWith('.html')) {
fileNames.push('./Funda/' + files[i]);
}
}
console.log(fileNames);
let jsonFile = './funda.json';
/**
* From all the filenames, parse the html and put the data in a json file
*/
for(let i = 0; i < fileNames.length; i++) {
console.log('Parsing file: ' + fileNames[i]);
let data = fs.readFileSync(fileNames[i], 'utf8');
if(i === 0) {
fs.writeFileSync(jsonFile, '[');
}
let $ = cheerio.load(data);
let length = $('.search-result-content-inner').length;
console.log('File-length: ' + length);
for(let j = 0; j < length; j++) {
let adres = getAdres($, j);
let postcodePlaats = getPostcodePlaats($, j);
let postcode = postcodePlaats.substring(0, 8);
let plaats = postcodePlaats.substring(8, postcodePlaats.length);
let oppervlakte = getOppervlakte($, j);
let url = getUrl($, j);
let json = JSON.stringify({'adres':adres, 'postcode':postcode, 'plaats':plaats, 'oppervlakte':oppervlakte, 'url':url});
fs.appendFileSync(jsonFile, json);
if(i === fileNames.length - 1 && j === length - 1) {
fs.appendFileSync(jsonFile, ']');
console.log('Finished creating Funda JSON file!');
} else {
fs.appendFileSync(jsonFile, ',');
}
}
}
//let pageToVisit = 'http://localhost:8080/ZZZTest/Bedrijfspand_Rijssen.html';
/*console.log('Visiting page ' + pageToVisit);
request(pageToVisit, (error, response, body) => {
if(error) {
console.log(error);
}
// Check status code (200 is HTTP OK)
console.log('Status code: ' + response.statusCode);
if(response.statusCode === 200) {
// Parse the document body
let $ = cheerio.load(body);
let length = $('.search-result-content-inner').length;
let fileName = './rijssen.json';
fs.writeFileSync(fileName, '[');
for(let i = 0; i < length; i++) {
let adres = getAdres($, i);
let postcodePlaats = getPostcodePlaats($, i);
let postcode = postcodePlaats.substring(0, 8);
let plaats = postcodePlaats.substring(8, postcodePlaats.length);
let oppervlakte = getOppervlakte($, i);
let url = getUrl($, i);
let json = JSON.stringify({'adres':adres, 'postcode':postcode, 'plaats':plaats, 'oppervlakte':oppervlakte, 'url':url});
fs.appendFileSync(fileName, json);
if(i < length-1) {
fs.appendFileSync(fileName, ',');
}
}
fs.appendFileSync(fileName, ']');
console.log('Success. File written to: ' + fileName);
}
});*/
function getUrl($, i) {
let url = $('.search-result-header');
return url[i].children[1].attribs.href;
}
function getOppervlakte($, i) {
let opp = $('.search-result-kenmerken');
return opp[i].children[0].next.children[1].children[0].data.trim();
}
function getPrijs($, i) {
let prijs = $('.search-result-price');
return prijs[i].children[0].data.trim();
}
function getPostcodePlaats($, i) {
let pp = $('.search-result-subtitle');
return pp[i].children[0].data.trim();
}
function getAdres($, i) {
let title = $('.search-result-title');
return title[i].children[0].data.trim();
}