Cheerio: is it possible to select and get html comments?

Created on 25 Mar 2014  路  8Comments  路  Source: cheeriojs/cheerio

i am searching for a node html parser which make it possible to get not only DOM nodes but html comments too in any way. is this possible`?

Most helpful comment

I can confirm that you can access the comments like so in older versions of Cheerio:

var $comments = $("*").contents().filter(function () {
    return this.nodeType === 8;
});

$comments.each(function(){
    // read the existing comment
    console.log(this.data);

    // change it to something else
    this.data = "something else";
});

What versions?

Seems to be working for me in "cheerio": "^1.0.0-rc.2"

All 8 comments

The DOM tree contains comment nodes, but there is no explicit way to query them from cheerio.

Does this not work:

var $comments = $( "body" ).contents().filter(function() {
    return this.nodeType === 8;
});

I haven't tested with nodeType 8 (comments), but I've used it plenty with nodeType 3.

I can confirm that you can access the comments like so in older versions of Cheerio:

var $comments = $("*").contents().filter(function () {
    return this.nodeType === 8;
});

$comments.each(function(){
    // read the existing comment
    console.log(this.data);

    // change it to something else
    this.data = "something else";
});

I can confirm that you can access the comments like so in older versions of Cheerio:

var $comments = $("*").contents().filter(function () {
    return this.nodeType === 8;
});

$comments.each(function(){
    // read the existing comment
    console.log(this.data);

    // change it to something else
    this.data = "something else";
});

What versions?

What versions?

I'm not sure. What version is it not working in?

I can confirm that you can access the comments like so in older versions of Cheerio:

var $comments = $("*").contents().filter(function () {
    return this.nodeType === 8;
});

$comments.each(function(){
    // read the existing comment
    console.log(this.data);

    // change it to something else
    this.data = "something else";
});

What versions?

Seems to be working for me in "cheerio": "^1.0.0-rc.2"

Seems to be working for me in "cheerio": "^1.0.0-rc.2"

Nice.

I'm using "cheerio": "^1.0.0-rc.3",, and I find out this works.

  const $ = cheerio.load(htmlData, { decodeEntities: false });

  $('body').contents().map((i, el) => {
    if (el.type === 'comment') {
      console.log(el.data)  // You can get the contents of html comment
      console.log($(el).html())  // This is null
      console.log($(el).text())  // This is ' '
    }
  })

CheerioElement type has .type property, which is defined as string, and it looks like the possible strings are 3, 'tag' | 'text' | 'comment'.

And I tried 3 ways and find that the contents of comment is assigned in el.data.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gajus picture gajus  路  4Comments

clayrisser picture clayrisser  路  4Comments

M3kH picture M3kH  路  4Comments

collegepinger picture collegepinger  路  3Comments

bxqgit picture bxqgit  路  3Comments