javascript - How to turn Cheerio DOM nodes back into html? -
using html below, i'm attempting extract html of each paragraph. cannot find way turn nodes html or query objects.
the below string var html = ...
<article>     <p> p1 </p>     <p> p2 </p> </article> the html loaded such
var $ = require('cheerio').load(html) var paragraphs = $('p').toarray().map(p => /* want html @ point */ ) how html of these paragraphs?
note: clarity i'm calling return value of cheerio.load "query object" , return of toarray method dom nodes; lack of better phrase.
you can use $.html: 
var paragraphs = $('p').toarray().map(p => {     console.log($.html(p));     return $.html(p); }); the documentation shows example using selector, cheerio dom elements work expected:
if want return outerhtml can use $.html(selector):
$.html('.pear') //=> <li class="pear">pear</li>
Comments
Post a Comment