So I have this simple code to mine some videos url in order to apply another scraping function to it afterward. My problem is that I can't seem to return the url-filled array. I know that it's a problem of scope but I'm not that familiar with Javascript and my knowledge got me as far as I could.
Here is the code :
var request = require('request');
var cheerio = require('cheerio');
var startUrl = 'http://www.somewebsite.com/mostviewed';
var getVideoIds = function(url) {
var urls = [];
request(url, function(err, resp, body){
if (err)
throw err;
$ = cheerio.load(body);
var videoUrls = [];
$('.videoTitle a').each(function() {
videoUrls.push($(this).attr('href'));
});
});
return urls;
}
var urlsToScrap = getVideoIds(startUrl);
console.log(urlsToScrap);
PS : the current code returns an empty array;