node.js - Parsing a specific table in a html page that has multiple tables using cheerio

Viewed 1409

Here is my sample html.

<table class = "one">
  <thead>..</thead>
  <tbody>
    <tr>
      <td>"Element 1<td>
    </tr>
  </tbody>
</table>
<table class = "one">
  <thead>..</thead>
  <tbody>
    <tr>
      <td>"Element 2<td>
      <td>"Element 3<td>          
    </tr>
  </tbody>
</table>

I am using cheerio to parse through the tables but unfortunately, I am not able to select a specific table as the tables do not have any ids or belong to different classes. The below code pulls all the rows from both the tables. I had to compare the output with known elements (for example "element2") to fix up an index that I can use to identify tables. But is there any better way using cheerio to pick up the first table on the page or just the second table on the page?

var $ = cheerio.load(sampleHTML);
$('table tr').each(function (i, element) { ..} 
1 Answers
Related