Javascript js解析HTML表并将结果作为JSON获取
我找到了cheerio库来解析HTML节点,获取表的子项并创建JSON格式,但我无法正确使用它,我的代码也无法获取节点 HTML 我在这行代码上没有得到任何结果:Javascript js解析HTML表并将结果作为JSON获取,javascript,html,node.js,dom,cheerio,Javascript,Html,Node.js,Dom,Cheerio,我找到了cheerio库来解析HTML节点,获取表的子项并创建JSON格式,但我无法正确使用它,我的代码也无法获取节点 HTML 我在这行代码上没有得到任何结果: log.info(data); 请尝试以下代码: request(url, function (error, response, html) { if (!error) { const $ = cheerio.load(html) const result = $(".ExRate-TR").map((i, el
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))
}
})
此日志记录:
[{"currency":"USD","amount":"12345"},{"currency":"CHF","amount":"78456"}]
谢谢你提到第n种类型,它对我真的很有帮助
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"}]