-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
79 lines (62 loc) · 2.05 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
let express = require('express');
let fs = require('fs');
let request = require('request');
let cheerio = require('cheerio');
let app = express();
const PORT = process.env.PORT || 5000
let link = [
{
"name": "storia"
}
]
app.use(express.static(__dirname + '/public'))
app.get("/", (req, res) => {
res.render("home.ejs", {
links: link
})
})
app.get("/storia", (req, res) => {
let listUrl = [], recentPost = "https://www.storia.ro/vanzare/apartament/?search%5Border%5D=created_at_first%3Adesc", listOfAds = []
request(recentPost, function(err, response, page) {
if(!err) {
let $ = cheerio.load(page)
let group = ""
let b =""
group = $("h3").children("a")
for(i = 0; i < group.length; i++) {
b = $(group)[i]
listUrl.push($(b).attr("href"));
}
listUrl.forEach((el, index) => {
request(el, (error, response, html) => {
if(!error) {
let $ = cheerio.load(html);
json = { title: "", price: "", priceCube: "", area: "", phone:"", floor: "", adress: "", description: "", picture: ""}
json.title = $("h1").text()
json.price = $(".param_price").children("span").children("strong").text()
json.priceCube = $(".param_price").contents()[2].nodeValue
json.area = $(".param_m").children("span").children("strong").text()
json.phone = $("phone-spoiler-number").text()
json.floor = $(".param_floor_no").children("span").text()
json.adress = $(".address-links").text()
json.description = $(".text-contents").text()
json.picture = $("a img").prop("src")
}
listOfAds.push(json)
if (parseInt(index) + 1 === parseInt(listUrl.length)) {
// fs.writeFile('output.json', JSON.stringify(listOfAds, null, 4), function(err){
// console.log('File successfully written! - Check your project directory for the output.json file');
// })
res.render("scrape.ejs", {
results : listOfAds,
name: "Storia"
})
}
})
})
}
})
})
app.listen(PORT)
console.log('Magic happens on port 8082');
exports = module.exports = app;