-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver1.js
51 lines (37 loc) · 1.4 KB
/
server1.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
var express = require('express');
var fs = require('fs');
var request = require('request');
var cheerio = require('cheerio');
var app = express();
app.get('/scrape', function(req, res){
url = 'http://www.imdb.com/title/tt1229340/';
request(url, function(error, response, html){
if(!error){
var $ = cheerio.load(html);
console.log(html);
var title, release_details, rating;
var json = { title : "", release_details : "", rating : ""};
$('.title_wrapper').filter(function(){
var data = $(this);
title = data.children().first().text();
release_details = data.children().last().children().text();
})
$('.ratingValue').filter(function(){
var data = $(this);
// The .star-box-giga-star class was exactly where we wanted it to be.
// To get the rating, we can simply just get the .text(), no need to traverse the DOM any further
rating = data.text();
json.rating = rating;
json.title = title;
json.release_details = release_details;
})
}
fs.writeFile('output1.json', JSON.stringify(json, null, 4), function(err){
console.log('File successfully written! - Check your project directory for the output.json file');
})
res.send('Check your console!')
})
})
app.listen('8081')
console.log('Magic happens on port 8081');
exports = module.exports = app;