Node.jsparsingHTML表格并以JSONforms获得结果

我发现cheerio库parsingHTML节点,并得到表的孩子,并创buildJSON格式,但我不能正确使用,我的代码无法点头。

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> 

在这个HTML我试图得到USD ,第一行1234节点,直到完成表,例如我的代码必须得到USD1234CHF78456

 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)); }); } }); 

我在这行代码中没有得到任何结果:

 log.info(data); 

试试这个代码:

 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)) } }) 

这logging:

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