Stuart Conroy Stuart Conroy - 4 months ago 19
Javascript Question

How can I scrape pages with Node.js

I am trying to scrape a website but I cannot get the results to write to an HTML file.

I used cheerio in node.js and my code is below.

var http = require('http');
var path = require('path');
var request = require('request');
var cheerio = require('cheerio');

http.createServer(function (req, res) {
res.write('<html><head></head><body>');
request('http://www.espn.com', function(err, res, html){
var $ = cheerio.load(html);

$('a.realStory').each(function(i, element) {
var node = $(this);
var text = node.text();
res.write('<p>'+ text +'</p>');
});
});

res.end('</body></html>');
}).listen(1337);


How can i run this file and then goto my localhost to view it?

Answer

Rudimentary Implementation:

var express = require('express'),
    path = require('path'),
    request = require('request'),
    cheerio = require('cheerio'),
    app = express();
app.get('/', function (req, res) {
    request('http://www.espn.com', function (e, r, html) {
        var $ = cheerio.load(html);
        $('a.realStory').each(function (i, element) {
            var node = $(this);
            var text = node.text();
            res.write('<p>' + text + '</p>');
        });
        res.end();
    });
});
app.listen(process.env.PORT || 1337, function () {
    console.log("Server running..");
});
Comments