Last active
August 29, 2015 14:17
-
-
Save bpevs/f67a3f2078b234764c73 to your computer and use it in GitHub Desktop.
A Simple Hacker News Scraper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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