-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
62 lines (55 loc) · 2.24 KB
/
server.js
File metadata and controls
62 lines (55 loc) · 2.24 KB
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
class ModelContext {
constructor(nameList, priceList) {
}
}
function parseModel() {
var modelContext = new ModelContext(undefined, undefined);
var request = require("request"),
cheerio = require("cheerio");
url = "http://www.mercedes-benz.ru/content/russia/mpc/mpc_russia_website/ru/home_mpc/passengercars/home/new_cars/model_overview.html"
request(url, function (error, response, body) {
var nameList = [];
var priceList = [];
if (!error) {
var $ = cheerio.load(body);
$('[class= "clearfix ms-showroom-size"]').find('li').each(function (i, el) {
$(el).find('li').each(function (j, element) {
var currentElementName = $(element).find('a > p').text();
var currentElementPrice = $(element).find('li > p').text();
if ((i + j) != 32 && (i + j) != 31 || currentElementName === null && currentElementPrice == undefined || currentElementName == "V-Êëàññ" && currentElementPrice == "V-Êëàññ") {
nameList[i + j] = currentElementName;
priceList[i + j] = currentElementPrice;
}
})
});
for (i = 0; i < nameList.length; i++) {
console.log(nameList[i] + " : " + priceList[i]);
}
modelContext.nameList = nameList;
modelContext.priceList = priceList;
} else {
console.log("Error: " + error);
}
});
return new ModelContext(nameList, priceList);
}
//nameList ïî÷åìó òî íå îïðåäåëåí
function createTable(nameList, priceList) {
var htmlString = "<table><tr><td>ClassName</td><td>Price</td></tr>";
for (i = 0; i < namelist.length; i++) {
htmlString += '<tr><td>' + nameList[i] + '</td><td>' + priceList[i] + '</td></tr>';
}
htmlString += " </table>";
return htmlString;
}
var express = require('express');
var app = express();
// http://localhost:8080/
app.get('/', function (req, res) {
var modelContext = parseModel();
res.write('<html><head></head><body>');
res.write(createTable(modelContext.nameList, modelContext.priceList));
res.end('</body></html>');
});
app.listen(8080);
console.log('Start server');