Here is my web-crawler with node.js using cheerio library:
var request = require('request');
var cheerio = require('cheerio');
var fs = require('fs');
var urls = [];
request('http://www.reddit.com', function(err, resp, body){
if(!err && resp.statusCode == 200){
var $ = cheerio.load(body);
$('a.title may-blank').each(function(){
var url = this.attr('href');
urls.push(url);
});
console.log(urls);
}
});
But when I run it I get the the following output:
[]
Instead of 25 links in the array.
What have I done wrong?
How can I fix that?