Reputation: 2064
I have a standard HTML table, but I cant seem to find anything in order to get a foothold and get started. My table is this:
<div id="seriesDiv" style="margin-top: 0px;">
<table class="tableFile2" summary="Results">
<tbody><tr>
<th scope="col" width="7%">Filings</th>
<th scope="col" width="10%">Format</th>
<th scope="col">Description</th>
<th scope="col" width="10%">Filing Date</th>
<th scope="col" width="15%">File/Film Number</th>
</tr>
<tr>
<td nowrap="nowrap">4</td>
<td nowrap="nowrap"><a href="/Archives/edgar/data/1087294/000120919119051031/0001209191-19-051031-index.htm" id="documentsbutton"> Documents</a></td>
<td class="small">Statement of changes in beneficial ownership of securities<br>Acc-no: 0001209191-19-051031 Size: 9 KB </td>
<td>2019-09-27</td>
<td></td>
</tr>
<tr class="blueRow">
<td nowrap="nowrap">4</td>
<td nowrap="nowrap"><a href="/Archives/edgar/data/1087294/000120919119050363/0001209191-19-050363-index.htm" id="documentsbutton"> Documents</a></td>
<td class="small">Statement of changes in beneficial ownership of securities<br>Acc-no: 0001209191-19-050363 Size: 50 KB </td>
<td>2019-09-20</td>
<td></td>
</tr>
So standard stuff -- table, tbody, trs and tds
I have tried
var rows = $("#seriesDiv").find("table");
console.log("in front loop ", rows.length);
for (var i = 0; rows.length; i++) {
console.log("inside loop");
}
and I tried:
var rows = $(".tableFile2").find("tr");
```
Can someone please show me how to do this? I want to loop through TRs, and for each TR, I want to loop through TDs.
thank you
Upvotes: 0
Views: 96
Reputation: 2174
The code below will help you loop through each tr. Also, take note that if you look for only "tds" for each "tr", you will not find "th". Thus, you also have to look for "ths". Take note that the code below is just an example. Thus, no error check or data evaluation or anything like that.
const cheerio = require('cheerio');
var html = `
<div id="seriesDiv" style="margin-top: 0px;">
<table class="tableFile2" summary="Results">
<tbody><tr>
<th scope="col" width="7%">Filings</th>
<th scope="col" width="10%">Format</th>
<th scope="col">Description</th>
<th scope="col" width="10%">Filing Date</th>
<th scope="col" width="15%">File/Film Number</th>
</tr>
<tr>
<td nowrap="nowrap">4</td>
<td nowrap="nowrap"><a href="/Archives/edgar/data/1087294/000120919119051031/0001209191-19-051031-index.htm" id="documentsbutton"> Documents</a></td>
<td class="small">Statement of changes in beneficial ownership of securities<br>Acc-no: 0001209191-19-051031 Size: 9 KB </td>
<td>2019-09-27</td>
<td></td>
</tr>
<tr class="blueRow">
<td nowrap="nowrap">4</td>
<td nowrap="nowrap"><a href="/Archives/edgar/data/1087294/000120919119050363/0001209191-19-050363-index.htm" id="documentsbutton"> Documents</a></td>
<td class="small">Statement of changes in beneficial ownership of securities<br>Acc-no: 0001209191-19-050363 Size: 50 KB </td>
<td>2019-09-20</td>
<td></td>
</tr>
`;
const $ = cheerio.load(html);
var tables = $("#seriesDiv").find("table");
var trs = $(tables).find("tr");
for ( let i = 0; i < trs.length; i++ ){
let tds = $(trs[i]).find("td");
let ths = $(trs[i]).find("th");
for ( let j = 0; j < ths.length; j++ ){
console.log($(ths[j]).text());
}
for ( let j = 0; j < tds.length; j++ ){
console.log($(tds[j]).text());
}
}
Upvotes: 2