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]);
}
);
});
asked Apr 20, 2016 at 14:50
lukassz
3,3408 gold badges38 silver badges77 bronze badges
1 Answer 1
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
})
answered Apr 20, 2016 at 16:18
Kuba Kutiak
921 silver badge3 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-js
async.serieshere? Also, you should always callcallbackinstead of jumping intoerrorHandlingright away.p[0], not overp.