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);
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..");
});