I'm building a scraper in Node, which uses request and cheerio to load in pages and parse them.
It's important that I put a callback only AFTER Request and Cheerio has finished loading the page. I'm trying to use the async
extension, but I'm not entirely sure where to put the callback.
request(url, function (err, resp, body) {
var $;
if (err) {
console.log("Error!: " + err + " using " + url);
} else {
async.series([
function (callback) {
$ = cheerio.load(body);
callback();
},
function (callback) {
// do stuff with the `$` content here
}
]);
}
});
I've been reading the cheerio documentation
and can't find any examples of callbacks for when the content has been loaded in.
What's the best way to do it? When I throw 50 URLs at the script it starts moving on too early before cheerio has properly loaded in content, and I'm trying to curb any errors with async loading.
I'm totally new to asynchronous programming and callbacks in general so if I'm missing something simple here please let me know.