You have two issues. One is that you're returning urls
but it's never set to anything. You are pushing values onto videoUrls
but you're returning the empty urls
array. The other is that request
is an asynchronous function. You will need to set a callback to set the video urls once it brings the scraped data back.
So:
var urls = [];
request(url, function(err, resp, body){
if (err)
throw err;
$ = cheerio.load(body);
$('.videoTitle a').each(function() {
urls.push($(this).attr('href'));
});
onVideosScraped();
});
function onVideosScraped() {
console.log(urls);
}
This should work, and is a rudimentary way to do it. You can of course wrap any of this you want in functions to make it more reusable, but I hope this answers your question.