0

I'm using express framework to grab links from some webpage, and I add this links to Array. I'm using async to print final result but when I print my array I gets list of objects.

Result of collate function:

Finall: [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Objec
t] 

How can I iterate of this array of objects? This is my code:

router.route('/send')
  .post(function(req, res){
      async.series([ function(callback){
            var url = req.body.url;
            var items = [];

            console.log(url);
            if(url.length>=1) {

              if (isURL(url)) {
                console.log('OK');
                res.sendStatus(200);

                request(url, function(err, resp, body){

                  $ = cheerio.load(body);
                  links = $('a.offer-title');
                  $(links).each(function(i, link){
                     items[i] = new itemParam($(link).text(),12)
                  });
                  callback(false, items);

                });

              } else {
                errorHandling(res, 401,"Invalid url");
              }
            }else{
                errorHandling(res, 401,"Invalid url");
            }
        }
      ],
        /*
         * Collate results
         */
        function(err, p) {
          console.log("Finall: " + p[0]);

       }
      );
  });
15
  • uh, have you tried a loop? Commented Apr 20, 2016 at 14:53
  • Yes I try with for loop. ` for (var key in p) { if (p.hasOwnProperty(key)) { console.log(key + " -> " + p[key]); } }` Commented Apr 20, 2016 at 14:54
  • 2
    WTH are you using async.series here? Also, you should always call callback instead of jumping into errorHandling right away. Commented Apr 20, 2016 at 14:54
  • I am new to this topic. What exactly should I use? Commented Apr 20, 2016 at 14:55
  • It's an array, so you should use an array loop. Also, it seems you need to iterate over p[0], not over p. Commented Apr 20, 2016 at 14:55

1 Answer 1

2

here's one simple way to do this:

const request = require('request'),
      cheerio = require('cheerio')

const scrape = (url) => {
    return new Promise((resolve, reject) => {
        let links = [] // collect all links here
        request(url, (err, res, body) => {
            if (err) {
                return reject(err)
            }
            let $ = cheerio.load(body)
            $('a').each(function () { // use any selector you like
                links.push($(this).attr('href')) // ...and extract whatever you like
            })
            resolve(links)
        })
    })
}

scrape('http://google.com?q=javascript')
  .then(links => {
      // handle links
      console.log(links)
  })
  .catch(err => {
      // handle error
  })
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.