Skip to content

Instantly share code, notes, and snippets.

@quanengineering
Created March 15, 2016 04:05
Show Gist options
  • Save quanengineering/78a8889797e3df030d3f to your computer and use it in GitHub Desktop.
Save quanengineering/78a8889797e3df030d3f to your computer and use it in GitHub Desktop.
How to use callback
function getArticleUrlPerPage(pageNumber, articleUrls, callback) {
var options = {
method: 'GET',
url: 'https://www.getnewsmart.com/?last_time_title=This+week&page=' + pageNumber + '&section=&xhr=true',
};
request(options, function(error, response, body) {
if (error) throw new Error(error);
$ = cheerio.load(body);
$('.text-wrapper').each(function(i, elem) {
articleUrls.push($(this).find('.pjax-link').attr('href'));
callback();
});
});
}
var articleUrls = [];
var numberOfArticle = 0;
function getArticleUrlWholeSite() {
console.log('Starting to get article urls');
for (var pageNumber = 1; pageNumber <= 2; pageNumber++) {
getArticleUrlPerPage(pageNumber, articleUrls, function(n) {
numberOfArticle++;
console.log('Number of Article: ' + numberOfArticle);
});
};
}
getArticleUrlWholeSite();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment