tux-world tux-world - 3 months ago 55
Node.js Question

Node.js parse HTML table and get result as JSON

I found cheerio library to parse HTML nodes and get children of table and create JSON format, but I can't use correctly that and my code could not get nods.

HTML



<table class="Modules" width="180" cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr>
<th align="center" style="padding:2px 5px;" colspan="6">
<span id="ctl00_ucBody_ucContent_ctl00_rptItemList_ctl00_lblTitle"
style="font-family: Arial, Tahoma, Helvetica, sans-serif; font-weight: bold; font-size : 1.1em; float:right;">نرخ ارز</span>
<span dir="ltr">
<span id="ctl00_ucBody_ucContent_ctl00_rptItemList_ctl00_lblDate"
style="font-family: Arial, Tahoma, Helvetica, sans-serif; font-weight: bold; font-size : 1.1em;">۱۳۹۵/۰۵/۲۸</span>
</span>
</th>
</tr>
<tr class="ExRate-TR">
<td>USD</td>
<td nowrap="">Dollar</td>
<td>12345</td>
<td>
<img width="9" height="9" title="" alt="down" src="/Images/down.gif">
</td>
<td>
<input id="ctl00_ucBody_ucContent_ctl00_rptItemList_ctl01_imgChart" />
</td>
<td>
<a id="ctl00_ucBody_ucContent_ctl00_rptItemList_ctl01_hypRSS" href="../ExRatesRSS.aspx?cid=1" alternatetext="RSS">
</td>
</tr>
<tr class="ExRate-TR">
<td>CHF</td>
<td nowrap="">Danmark</td>
<td>78456</td>
<td>
<img width="9" height="9" title="" alt="down" src="/Images/down.gif">
</td>
<td>
<input id="ctl00_ucBody_ucContent_ctl00_rptItemList_ctl01_imgChart" />
</td>
<td>
<a id="ctl00_ucBody_ucContent_ctl00_rptItemList_ctl01_hypRSS" href="../ExRatesRSS.aspx?cid=1" alternatetext="RSS">
</td>
</tr>
</tbody>
</table>


In this HTML I'm trying to get
USD
,
1234
nodes on first row until finish table, for example in that my code must get
USD
,
1234
and
CHF
,
78456
.

request(url, function (error, response, html) {
if (!error) {
var $ = cheerio.load(html);
var title, release, rating;
var json = {currency: "", amount: ""};
$('.Modules').filter(function () {
var data = $(this);
log.info(data);
currency = data.children().first().text();
amount = data.children().next().next().children().text();
json.currency = currency;
json.amount = amount;
log.info(JSON.stringify(json));
});
}
});


I don't get any result on this line of code:

log.info(data);

Answer

Try this code:

request(url, function (error, response, html) {
  if (!error) {
    const $ = cheerio.load(html)
    const result = $(".ExRate-TR").map((i, element) => ({
      currency: $(element).find('td:nth-of-type(1)').text().trim()
     ,amount: $(element).find('td:nth-of-type(3)').text().trim()
    })).get()
    console.log(JSON.stringify(result))
  }
})

This logs:

[{"currency":"USD","amount":"12345"},{"currency":"CHF","amount":"78456"}]