I'm implementing a web scraper using NodeJS with the Request and Cheerio libraries. I'm trying to save the scraped URL links into an array, however, for some reason my array ends up becoming undefined when I attempt to export it.
The console.log(url_dict) towards the end prints the data to terminal, but if I export the module to another .js file and print it to terminal (with console.log), I get an undefined error.
Any thoughts? Thanks so much for your time! :)
var request = require('request');
var cheerio = require('cheerio');
var senatorlist = 'http://en.wikipedia.org/wiki/Seniority_in_the_United_States_Senate';
var url_dict = [];
function lister() {
request(senatorlist, function(err, resp, body) {
if (err)
throw err;
var $ = cheerio.load(body);
$('table.wikitable tr a').each(function(i, link){
url_dict.push($(link).attr('href'));
});
console.log(url_dict);
});
}