Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/428.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 用cheerio刮掉所有元素_Javascript_Node.js_Cheerio - Fatal编程技术网

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