0

I have a hard time finding out how to iterate the children of paragraph elements using Cheerio.

<html>
<head>
</head>
<body>
<p>Hello, this is me - Daniel</p>
<p><strong>Hello</strong>, this is me - Daniel</p>
<p>Hello, <strong>this is me</strong> - Daniel</p>
<p>Hello, this is me - <strong>Norbert</strong></p>
<p><strong>Hello</strong>, this is me - <strong>Daniel</strong></p>
</body>
</html>

Using find('*') or children('*'), Cheerio only returns the <strong> tags but not the plain-text one's.

What I need is a list of all nested elements (beneath <p>) including the plain text.

isherwood
  • 58,414
  • 16
  • 114
  • 157
  • Does this answer your question? [Get text in parent without children using cheerio](https://stackoverflow.com/questions/20832910/get-text-in-parent-without-children-using-cheerio) – ggorlen Apr 24 '23 at 15:35
  • Related questions: [1](https://stackoverflow.com/questions/20832910/get-text-in-parent-without-children-using-cheerio), [2](https://stackoverflow.com/questions/54878673/cheerio-get-normal-text-nodes), [3](https://stackoverflow.com/questions/57148932/how-to-get-childnodes-of-a-div-in-cheerio), [4](https://stackoverflow.com/questions/42235516/get-the-text-of-the-current-node-only), [5](https://stackoverflow.com/questions/74418220/how-do-i-get-text-after-single-br-tag-in-cheerio/74418510#74418510), [6](https://stackoverflow.com/questions/73690939) – ggorlen Apr 24 '23 at 16:01
  • Please revise your post title to ask a clear, specific question. Also, you're expected to make an effort and show your code. See [ask]. – isherwood Apr 24 '23 at 16:09

1 Answers1

1

Try contents():

const cheerio = require("cheerio"); // 1.0.0-rc.12

const html = `
<p>Hello, this is me - Daniel</p>
<p><strong>Hello</strong>, this is me - Daniel</p>
<p>Hello, <strong>this is me</strong> - Daniel</p>
<p>Hello, this is me - <strong>Norbert</strong></p>
<p><strong>Hello</strong>, this is me - <strong>Daniel</strong></p>
`;
const $ = cheerio.load(html);
const text = [...$("p").contents()].map(e => $(e).text());
console.log(text);

Output:

[
  'Hello, this is me - Daniel',
  'Hello',
  ', this is me - Daniel',
  'Hello, ',
  'this is me',
  ' - Daniel',
  'Hello, this is me - ',
  'Norbert',
  'Hello',
  ', this is me - ',
  'Daniel'
]
ggorlen
  • 44,755
  • 7
  • 76
  • 106