【问题标题】:Node.js parse HTML table and get result as JSONNode.js 解析 HTML 表并以 JSON 格式获取结果
【发布时间】:2016-12-25 05:37:49
【问题描述】:

我找到了 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);

【问题讨论】:

    标签: javascript html node.js dom cheerio


    【解决方案1】:

    试试这个代码:

    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"}]
    

    【讨论】:

    • 感谢您提到第 n 个类型,这对我很有帮助
    猜你喜欢
    • 1970-01-01
    • 2014-11-19
    • 1970-01-01
    • 2015-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多