CheerioJS by going through a <ul> with the same class name
I am trying to go through each <ul>
and get the meaning of each <li>
. The fact is that it only accepts the first <ul>
and skips the rest.
Html
<div id="browse-results">
<ul class="tips cf">
<li>tip11</li>
<li>tip12</li>
<li>tip13</li>
</ul>
<ul class="tips cf">
<li>tip21</li>
<li>tip22</li>
<li>tip23</li>
</ul>
<ul class="tips cf">
<li>tip31</li>
<li>tip32</li>
<li>tip33</li>
</ul>
<ul class="tips cf">
<li>tip41</li>
<li>tip42</li>
<li>tip43</li>
</ul>
</div>
Cheerio parsing
$('#browse-results').find('.tips.cf').each(function(i, elm) {
console.log($(this).text()) // for testing do text()
});
$('#browse-results').children().find('.tips').each(function(i, elm) {
console.log($(this).text())
});
I've tried many more
The conclusion is only the values ββof the first <ul>
.
tip11
tip12
tip13
Please note guys that this is just an example of a snippet with the same structure as I am trying to parse.
I've spent almost 2 hours on this and I can't seem to find a way to do it.
source to share
Try the following:
var cheerio = require('cheerio');
var html = '<div id="browse-results"> \
<ul class="tips cf"> \
<li>tip11</li> \
<li>tip12</li> \
<li>tip13</li> \
</ul> \
<ul class="tips cf"> \
<li>tip21</li> \
<li>tip22</li> \
<li>tip23</li> \
</ul> \
<ul class="tips cf"> \
<li>tip31</li> \
<li>tip32</li> \
<li>tip33</li> \
</ul> \
<ul class="tips cf"> \
<li>tip41</li> \
<li>tip42</li> \
<li>tip43</li> \
</ul> \
</div>';
var $ = cheerio.load(html);
$('#browse-results li').each(function(i, elm) {
console.log($(this).text()) // for testing do text()
});
This will select all items li
that are decients #browse-results
.
source to share