After scraping a page , I have selected the footer of a table using cheerio with:
const $ = cheerio.load(data);
const foot = $('#tblAcctBal > tfoot > tr');
o = $(foot).html();
console.log(o);
results in the following html:
tr> <th rowspan="1" colspan="1"></th>
<th rowspan="1" colspan="1"></th>
<th rowspan="1" colspan="1"></th>
<th rowspan="1" colspan="1"></th>
<th rowspan="1" colspan="1"></th>
<th rowspan="1" colspan="1">$0.00</th>
<th rowspan="1" colspan="1">$0.00</th>
<th rowspan="1" colspan="1">$0.00</th>
<th rowspan="1" colspan="1">$0.00</th>
<th rowspan="1" colspan="1">$0.00</th>undefined</tr>n
I’m trying to get an array of the text values in the footer. I’ve tried:
$(foot).each( function (th) {
console.log($(th).text().trim())
})
but I’m getting no output. How do I fix this?
2
Answers
Select the
th
elements and loop over those.As a side note,
.each()
and other iterating functions in cheerio supply both theindex
andelement
in the function signature.In the example code, the selector returns a single
tr
element, which would need something like.children()
to get eachth
element.if they’re really there you can just do: