【发布时间】: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 中,我试图在第一行获取 USD、1234 节点直到完成表,例如,我的代码必须获取 USD、1234 和 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));
});
}
});
这行代码我没有得到任何结果:
log.info(data);
【问题讨论】:
标签: javascript html node.js dom cheerio