javascript - 使用 Cheerio 和 jsonframe 抓取时,获取 TypeError : selector. includes is not a function

标签 javascript node.js cheerio jsonframe-cheerio

我正在尝试使用以下代码废弃网站:

const cheerio = require('cheerio');
const jsonframe = require('jsonframe-cheerio');

const $ = cheerio.load('https://coinmarketcap.com/all/views/all/');
jsonframe($); // initializes the plugin

//exception handling 
process.on('uncaughtException', err =>
  console.error('uncaught exception: ', err))
process.on('unhandledRejection', (reason, p) =>
  console.error('unhandled rejection: ', reason, p))

const frame = {
    "crypto": {         
        "selector": "tbody > tr",   
        "data": [{             
            "name": "td:nth-child(2) > a:nth-child(3)", 
            "url": {                                  
                "selector": "td:nth-child(2) > a:nth-child(3)",    
                "attr": "href"                     
            },
            "marketcap": "tr > td:nth-child(4)",
            "price": "tr > td:nth-child(5) > a:nth-child(1)", 
        }]
    }

};

let companiesList = $('tbody').scrape(frame);
console.log(companiesList); 

但是,我在运行上述示例代码时收到 UnhandledPromiseRejectionWarning:

(node:3890) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: selector.includes is not a function

任何建议我做错了什么?

感谢您的回复!

更新

我将代码更改为以下内容。但是,我只能废弃第一个元素。

有什么建议为什么其他元素没有被废弃?

const cheerio = require('cheerio')
const jsonframe = require('jsonframe-cheerio')
const got = require('got');


async function scrapCoinmarketCap() {
    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
    }))
}

scrapCoinmarketCap()

最佳答案

根据您更新的代码,您可以通过迭代每个 tr 来抓取所有货币数据:

$('body tr').each(function() {
  console.log($(this).scrape(frame, {
    string: true
  }))
})

但是,我认为最简洁的方法(正如我在 another 中所说的那样)是使用 jsonframe-cheerio List/Array框架模式,正是为了做到这一点:

let frame = {
  currency: {
    _s: "tr",  // the selector
    _d: [{  // allow you to get an array of data, not just the first item
      "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 和 jsonframe 抓取时,获取 TypeError : selector. includes is not a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46882782/

相关文章:

node.js - 使用 Google Cloud Functions 确定点是否位于多边形中的简单方法

javascript - Cheerio 中的 DOM 选择器不起作用

javascript - 来自函数的一次异步执行的数据与该函数的另一次执行混合

javascript - Node.js fs Cheerio 读写多个文件

javascript - 在 DOM 中记录或绘制自定义事件模型的正确方法

Javascript - 排列和可能性

node.js - 蒙戈错误: connection timed out

Javascript,函数内部函数问题

javascript - 单击鼠标隐藏div

javascript - 在 Node.js 中使用 path.join 作为 __dirname 会造成混淆