Cheerio gets an external html element

I am using cheerio to parse a server side html file using node 6.10.2. I need to get outerHtml of each div inside the body of the document, and my code:

 /* const data is valid html document (type of string)*/
 const $ = cheerio.load(data);
 let pages = $('body > div').toArray();
 console.log(pages[0]); // Elements parsed correctly
 let htmlPages = pages.map(page => $(page).html());
 console.log(htmlPages[0]); // Here I have innerHtml, not outer...      

Problem: I get a string with innerHtml. Can someone help pls.

+4
source share
2 answers

Change the map function to

let htmlPages = pages.map(page => $.html(page));

according to docs

+6
source

If you don’t have easy access to the original $ object, then it also works

function outerHTML (element) {
    var index = element.index();
    var parent = element.parent().clone();
    var child = parent.children()[index];
    parent.empty();
    parent.append(child);
    return parent.html();
}
0
source

Source: https://habr.com/ru/post/1674642/


All Articles