Skip to content

Instantly share code, notes, and snippets.

@bpevs
Last active August 29, 2015 14:17
Show Gist options
  • Save bpevs/f67a3f2078b234764c73 to your computer and use it in GitHub Desktop.
Save bpevs/f67a3f2078b234764c73 to your computer and use it in GitHub Desktop.
A Simple Hacker News Scraper
//Include our Libraries!
var request = require('request');
var cheerio = require('cheerio');
//Request will send our web request to Hacker News
request('https://news.ycombinator.com', function (error, response, body) {
//continue with our scraper if there's no errors
if (!error && response.statusCode == 200) {
//lets cheerio parse our web response, and hand us pretty nodes!
//It adds it all to the dolla sign variable, so we can use it just like jQuery!!!
var $ = cheerio.load(body);
//for each post...
$('span.comhead').each(function(i, element){
//format all our new information!!!
var a = $(this).prev();
var data = {
rank: parseInt(a.parent().parent().text()),
title: a.text(),
url: a.attr('href')
};
console.log(data);
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment