Advertisement
Guest User

Untitled

a guest
Jan 21st, 2017
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. var express = require('express');
  2. var fs = require('fs');
  3. var request = require('request');
  4. var cheerio = require('cheerio');
  5. var app = express();
  6.  
  7. app.get('/scrape', function(req, res){
  8. // Let's scrape Anchorman 2
  9. url = 'http://www.imdb.com/title/tt1475582/';
  10.  
  11. request(url, function(error, response, html){
  12. if(!error){
  13. var $ = cheerio.load(html);
  14.  
  15. var title, release, rating;
  16. var json = { title : "", release : "", rating : ""};
  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();
  22.  
  23. json.title = title;
  24. json.release = release;
  25. })
  26.  
  27. $('.ratingValue').filter(function(){
  28. var data = $(this);
  29. rating = data.text().trim();
  30.  
  31. json.rating = rating;
  32. })
  33. }
  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. })
  38.  
  39. res.send('Check your console!')
  40. })
  41. })
  42.  
  43. app.listen('8081')
  44. console.log('Magic happens on port 8081');
  45. exports = module.exports = app;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement