Ann*_*lee 9 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); 
Run Code Online (Sandbox Code Playgroud)
但是,我UnhandledPromiseRejectionWarning在运行上面的示例代码时得到了一个:
(node:3890) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: selector.includes is not a function
Run Code Online (Sandbox Code Playgroud)
有什么建议我做错了吗?
感谢您的回复!
UPDATE
我将代码更改为以下内容.但是,我只能废弃第一个元素.
有什么建议为什么其他元素不会被废弃?
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()
Run Code Online (Sandbox Code Playgroud)
    根据您更新的代码,您可以通过迭代每个货币数据来刮取所有货币数据tr:
$('body tr').each(function() {
  console.log($(this).scrape(frame, {
    string: true
  }))
})
Run Code Online (Sandbox Code Playgroud)
但是,我认为最简洁的方法(正如我在另一个答案中所说)是使用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
}))
Run Code Online (Sandbox Code Playgroud)
        Rob*_*ann -1
该方法cheerio.load()不接受 URL -它需要 HTML 作为字符串。
虽然我没有研究过 Cheerio 的源代码,但该模块似乎试图将 URL 解析为 HTML 文档,显然,这失败了,并且开始出现各种错误。
要解决此问题,您需要首先将该 URL 的 HTML 内容加载到变量中,然后将该 HTML 内容传递给 Cheerio。
这是使用加载页面的示例got:
const got = require('got')
const cheerio = require('cheerio')
got('https://google.com')
.then(res => {
  const $ = cheerio.load(res.body)
  // Continue as usual
})
.catch(console.error)
Run Code Online (Sandbox Code Playgroud)
        |   归档时间:  |  
           
  |  
        
|   查看次数:  |  
           973 次  |  
        
|   最近记录:  |