Pruthvi Hingu
Pruthvi Hingu

Reputation: 282

why cheerio.load() function is not working properly

const cheerio = require('cheerio');

const HTML = `<tbody>
<tr>
    <th>
        Team Name
    </th>
    <th>
        Year
    </th>
    <th>
        Wins
    </th>
    <th>
        Losses
    </th>
</tr>
</tbody>
`;

const $ = cheerio.load(HTML);
const text = $('tr').text();
console.log(text);

Here I am not getting a text. Why?? Is there any error in the HTML string? I am not able to identify the issue. Please help!!!

Upvotes: 1

Views: 1186

Answers (1)

Quentin
Quentin

Reputation: 943645

A <tbody> element must be a child of a <table> element and yours isn't.

In the current version of Cheerio the parser doesn't recover from that error. Write valid HTML. (Wrapping your string in <table> and </table> is sufficient).

(If you downgrade to Cheerio 0.22.0 then the parser there can handle it, but fixing the input is preferred to using out of date libraries).

Upvotes: 6

Related Questions