1

I started build nodejs app to scrape data from a site that has pagination. I write for loop to get all page and push all url to store in an array. Inside that for loop I write async.eachSeries to run all url one by one and after it finish running I want to print out success message. But I don't know how to put that together to make it works properly. Here is my code

for(var pageNo = 1; pageNo <=200; pageNo++){
  siterequest('https://www...../en/jobs/?by=&in=&page='+pageNo, function(err, res, body){
    if(!err && res.statusCode ==200){
        var $ = cheerio.load(body);
        var links = [];
        $('.headline3.job-name-title > strong > a').each(function(i, elem){
            links.push('https://www......com.kh/'+$(elem).attr('href'));
        });
        async.eachSeries(links, function(uri, next){
            console.log('i will go this '+uri);
            next();
        }, function(callback){
            console.log('I did it');
        });
      };
  });
};

The code above did not work for me. Please help me! Thanks in advance

1
  • remove semicolon at the end of for loop Commented Feb 12, 2016 at 6:30

1 Answer 1

2

As you are using async.js you can try async.times instead of for loop

async.times(200, function (paneNo, nextPage) {
  siterequest('https://www...../en/jobs/?by=&in=&page=' + (pageNo + 1), 
    function (err, res, body) {
      if (!err && res.statusCode ==200) {
        var $ = cheerio.load(body);
        var links = [];

        $('.headline3.job-name-title > strong > a').each(function (i, elem) {
          links.push('https://www......com.kh/'+$(elem).attr('href'));
        });

        async.eachSeries(links, function (uri, next) {
          console.log('i will go this '+ uri);
          next();
        }, function(callback) {
          nextPage();
        });
      } else {
        nextPage(err);
      }
  });
}, function(err) {
  console.log('Done!');
});
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.