audiac-scrape
Version:
A simple web scraper.
24 lines (19 loc) • 694 B
JavaScript
var cheerio = require('cheerio');
var request = require('superagent');
module.exports = function(url, selector, callback){
request(url)
.end(function(err, reply){
// Node-style error handling and forwarding
if (err) return callback(err);
// Also handle/forward error if server returns error
if (reply.error) return callback(new Error(reply.text));
// Everything okay, load the HTML
var $ = cheerio.load(reply.text);
// Find interesting bits via selector and convert to array
var data = $(selector).map(function(){
return $(this).text();
}).toArray();
// Pass data back to the callback in second argument
callback(null, data);
});
}