Stuart Conroy Stuart Conroy - 1 year ago 85
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) {
request('', 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>');


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

Answer Source

Rudimentary Implementation:

var express = require('express'),
    path = require('path'),
    request = require('request'),
    cheerio = require('cheerio'),
    app = express();
app.get('/', function (req, res) {
    request('', 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>');
app.listen(process.env.PORT || 1337, function () {
    console.log("Server running..");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download