Aditya
Aditya

Reputation: 986

Parsing Table with Cheerio

<html>
<body>
    <table>
        <tbody>
            <tr>
                <!-- More things here -->
            </tr>
            <tr>
                <td>
                    <table></table>
                    <table>
                        <tbody>
                            <tr>
                                <td></td>
                                <td>I want this</td>
                            </tr>
                        </tbody>
                    </table>
                    <table></table>
                </td>
            </tr>
            <tr>
               <!-- More things here -->
            </tr>
        </tbody>
    </table>
</body>
</html>

So this is the weird looking HTMl structure. The element I want is the '' element which I have marked in the HTML.

This is the Node Js code I am using to parse the HTML. I am not very familiar with jQuery.

const $ = cheerio.load(body);
   $('table').find('tbody tr:nth-child(1)').each(()=> {
       $('td').find('table').each(() => {
            console.log($(this).text())
        })
   })

Upvotes: 2

Views: 7118

Answers (1)

A. Iglesias
A. Iglesias

Reputation: 2675

If the structure you show is fixed (number of rows, etc.)...

$('table tr:nth-child(2) table:nth-child(2) tr td:nth-child(2)').text()

I hope it helps

Upvotes: 4

Related Questions