You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import * as cheerio from 'cheerio';
const res = await fetch('https://example.com');
const body = await res.text();
const $ = cheerio.load(body);
console.log(JSON.stringify($('p').text()));
The output is:
This domain is for use in illustrative examples in documents. You may use this\n domain in literature without prior coordination or asking for permission.
The newline character followed by excessive spacing is present only in the HTML source. Generally the innerText will render this property, e.g. use Chrome to visit example.com and select the same element, then try $0.innerText and you'll get:
This domain is for use in illustrative examples in documents. You may use this domain in literature without prior coordination or asking for permission.
The text was updated successfully, but these errors were encountered:
Both are equivalent when outputting HTML. Cheerio implements the serialisation part of the HTML spec, which leads to code like this. Changing it leads to incompatibilities.
You can manually fix whitespace using .replace(/\s+/g, ' ').
The output is:
The newline character followed by excessive spacing is present only in the HTML source. Generally the innerText will render this property, e.g. use Chrome to visit example.com and select the same element, then try
$0.innerText
and you'll get:The text was updated successfully, but these errors were encountered: