Node.js解析HTML表并将结果作为JSON获取

时间:2016-08-18 10:19:44

标签: javascript html node.js dom cheerio

我找到cheerio库来解析HTML节点并获取表的子节点并创建JSON格式,但是我无法正确使用它并且我的代码无法点头。

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中,我试图在第一行获得USD1234个节点,直到完成表格,例如我的代码必须得到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);

1 个答案:

答案 0 :(得分:4)

试试这段代码:

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

此日志:

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