How to extract text separated by different HTML tags in Cheerio
P粉141911244
2023-08-13 17:01:06
<p>I'm trying to extract the following specific text strings as separate outputs, for example (grabbing them from the HTML below): </p>
<pre class="brush:js;toolbar:false;">let text = "This is the first text I need";
let text2 = "This is the second text I need";
let text3 = "This is the third text I need";
</pre>
<p>I really don't know how to get text separated by different HTML tags. </p>
<pre class="brush:html;toolbar:false;"><p>
<span class="hidden-text"><span class="ft-semi">Count:</span>31<br></span>
<span class="ft-semi">Something:</span> This is the first text I need
<span class="hidden-text"><span class="ft-semi">Something2:</span> </span>This is the second text I need
<br><span class="ft-semi">Something3:</span> This is the third text I need
</p>
</pre>
<p><br /></p>
Try something like this and see if it works:
Using your example html, the output should be:
You can iterate over the child nodes of
for any non-empty content<p>
and get thenodeType === Node.TEXT_NODE
: