Sivakumar Sivaraman Sivakumar Sivaraman - 1 month ago 10
Javascript Question

html scrapping for td element using cheerio js and node js

I have many html files with the below structure. I need to get the values 'GET' and 'http://localhost:5601/app/sense' from the below. but they are not exact same values in the all the documents, it be post, put or delete. but the html structure is same.

<colgroup>
<col class="col-lg-1">
<col class="col-lg-7">
</colgroup>
<tbody>
<tr>
<td>
<code>Method</code>
</td>
<td>GET</td>
</tr>
<tr>
<td>
<code>URL Path &amp; Params</code>
</td>
<td>http://localhost:5601/app/sense</td>
</tr>
</tbody>
</table>


Any suggestion on how to do this with cheerio ? I am trying to parse through the HTML table searching for the method and URL values in td element but I no luck.

glob(__dirname + "/../docs/*/*/*/*/*/*.html", function (er, files) {
for (var i = 0; i < files.length; i++) {

fs.readFile(files[i], (err, data) => {
if (err) throw err;
$ = cheerio.load(data);
$('tr').each(function () {
console.log($('td').val());
});
});

}
});

Answer

You have to target each TD and get the text, it has no value

glob(__dirname + "/../docs/*/*/*/*/*/*.html", function (er, files) {
    for (var i = 0; i < files.length; i++) {

        fs.readFile(files[i], (err, data) => {
            if (err) throw err;
            var $ = cheerio.load(data);

            var method = $('tr').eq(0).find('td').eq(1).text();
            var url    = $('tr').eq(1).find('td').eq(1).text();

        });

    }
});