Reputation: 409
I need to get all <a>
tag url from given webpage. And also I need to avoid <a>
tag between header and footer tags. I am loading body tag html but without header tag. Here is my code but it doesn't work.
var $ = cheerio.load(html);
$ = cheerio.load($('body').not('header'));
var links = $("a']");
links.each(function() {
console.log($(this).attr('href'));
});
If above code is wrong please suggest how to do that?
Upvotes: 1
Views: 2120
Reputation: 409
I did like this now its working fine ... Can any one tell me is this right way do this ?...
var $ = cheerio.load(body);
var t = $('body');
t.children('header').remove();
t.children('footer').remove();
var t = $.html(t);
var $ = cheerio.load(t);
var links = $("a");
links.each(function() {
console.log($(this).attr('href'));
});
Thanks,
Upvotes: -1
Reputation: 1175
I think the error was because you weren't loading the HTML in your second load. You were loading the body object. You should be able to do it this way:
var $ = cheerio.load(html);
$ = cheerio.load($('body').html());
$('header').remove();
console.log($.html());
Upvotes: 1
Reputation: 32137
Cheerio works just like jQuery.
var $ = cheerio.load(html);
var links = $('body').not('header').find('a');
// $('body:not(header) a') may also work.
links.each(function() {
console.log(this.href);
});
Upvotes: 3