Javascript 用cheerio刮掉所有元素
我正在运行下面的代码来刮取数据。但是,代码只会刮取第一个元素Javascript 用cheerio刮掉所有元素,javascript,node.js,cheerio,Javascript,Node.js,Cheerio,我正在运行下面的代码来刮取数据。但是,代码只会刮取第一个元素 const cheerio = require('cheerio') const jsonframe = require('jsonframe-cheerio') const got = require('got'); async function scrapeCoinmarketCap() { const url = 'https://coinmarketcap.com/all/views/all/' const
const cheerio = require('cheerio')
const jsonframe = require('jsonframe-cheerio')
const got = require('got');
async function scrapeCoinmarketCap() {
const url = 'https://coinmarketcap.com/all/views/all/'
const html = await got(url)
const $ = cheerio.load(html.body)
jsonframe($) // initializing the plugin
let frame = {
"Coin": "td.no-wrap.currency-name > a",
"url": "td.no-wrap.currency-name > a @ href",
"Symbol": "td.text-left.col-symbol",
"Price": "td:nth-child(5) > a",
}
console.log($('body').scrape(frame, {
string: true
}))
}
scrapeCoinmarketCap()
//Output -> only the first element
// {
// "Coin": "Bitcoin",
// "url": "/currencies/bitcoin/",
// "Symbol": "BTC",
// "Price": "$6122.67"
// }
有什么建议我做错了什么
谢谢你的回复 您可以使用以下模式获取所有货币数据:
我看不出这样做有什么意义。如果你用常规的方法刮它,你可以在你的浏览器控制台上测试它,并调整它直到你把它弄对。
let frame = {
currency: {
_s: "tr",
_d: [{
"Coin": "td.no-wrap.currency-name > a",
"url": "td.no-wrap.currency-name > a @ href",
"Symbol": "td.text-left.col-symbol",
"Price": "td:nth-child(5) > a"
}]
}
}
console.log($('body').scrape(frame, {
string: true
}))