|
1 | 1 | var express = require('express');
|
2 |
| -var fs = require('fs'); |
| 2 | +var fs = require('fs'); |
3 | 3 | var request = require('request');
|
4 | 4 | var cheerio = require('cheerio');
|
5 | 5 | var app = express();
|
6 | 6 |
|
7 | 7 | app.get('/scrape', function(req, res){
|
8 |
| - // Let's scrape Anchorman 2 |
9 |
| - url = 'http://www.imdb.com/title/tt1229340/'; |
| 8 | + // Let's scrape Anchorman 2 |
| 9 | + url = 'http://www.imdb.com/title/tt1229340/'; |
10 | 10 |
|
11 |
| - request(url, function(error, response, html){ |
12 |
| - if(!error){ |
13 |
| - var $ = cheerio.load(html); |
| 11 | + request(url, function(error, response, html){ |
| 12 | + if(!error){ |
| 13 | + var $ = cheerio.load(html); |
14 | 14 |
|
15 |
| - var title, release, rating; |
16 |
| - var json = { title : "", release : "", rating : ""}; |
| 15 | + var title, release, rating; |
| 16 | + var json = { title : "", release : "", rating : ""}; |
17 | 17 |
|
18 |
| - $('.title_wrapper').filter(function(){ |
19 |
| - var data = $(this); |
20 |
| - title = data.children().first().text().trim(); |
21 |
| - release = data.children().last().children().last().text().trim(); |
| 18 | + $('.title_wrapper').filter(function(){ |
| 19 | + var data = $(this); |
| 20 | + title = data.children().first().text().trim(); |
| 21 | + release = data.children().last().children().last().text().trim(); |
22 | 22 |
|
23 |
| - json.title = title; |
24 |
| - json.release = release; |
25 |
| - }) |
| 23 | + json.title = title; |
| 24 | + json.release = release; |
| 25 | + }) |
26 | 26 |
|
27 |
| - $('.ratingValue').filter(function(){ |
28 |
| - var data = $(this); |
29 |
| - rating = data.text().trim(); |
| 27 | + $('.ratingValue').filter(function(){ |
| 28 | + var data = $(this); |
| 29 | + rating = data.text().trim(); |
30 | 30 |
|
31 |
| - json.rating = rating; |
32 |
| - }) |
33 |
| - } |
| 31 | + json.rating = rating; |
| 32 | + }) |
| 33 | + } |
34 | 34 |
|
35 |
| - fs.writeFile('output.json', JSON.stringify(json, null, 4), function(err){ |
36 |
| - console.log('File successfully written! - Check your project directory for the output.json file'); |
37 |
| - }) |
| 35 | + fs.writeFile('output.json', JSON.stringify(json, null, 4), function(err){ |
| 36 | + console.log('File successfully written! - Check your project directory for the output.json file'); |
| 37 | + }) |
38 | 38 |
|
39 |
| - res.send('Check your console!') |
40 |
| - }) |
| 39 | + res.send('Check your console!') |
| 40 | + }) |
41 | 41 | })
|
42 | 42 |
|
43 | 43 | app.listen('8081')
|
44 | 44 | console.log('Magic happens on port 8081');
|
45 |
| -exports = module.exports = app; |
| 45 | +exports = module.exports = app; |
0 commit comments