javascript - 用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 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"
//    }

有什么建议我做错了什么吗?

感谢您的回复!

最佳答案

您可以通过 List / Array 获取所有货币数据图案:

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

关于javascript - 用cheerio刮掉所有元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47012544/

相关文章:

javascript - Bootstrap 3 Modal Event (Hidden.Bs.Modal) 不断重复

javascript - 在 JS 中拆分 Sprite Sheets/Chip Sets 的高性能方法是什么?

node.js - 在 node.js 中运行 sqlite3 查询返回 `undefined` 行,没有错误

javascript - 抓取谷歌新闻

javascript - 在页面加载几秒钟后检索页面的 html 内容

javascript - 多段线不显示,当切成较小的线时它确实显示

javascript - 有没有办法覆盖浏览器点击散列的默认行为?

javascript - Puppeteer 获取有关页面加载的信息 - 加载的文件列表及其大小

javascript - Firebase 函数 - 在 onWrite() 触发器中找不到父属性

javascript - 使用更多元生成器标签解析 html head