2017-10-30 3 views
0

私は以下のコードを実行してデータをスクラップしています。ただし、コードは最初の要素のみをスクラップします。cheerioですべての要素を解読

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

//Output -> only the first element 
// { 
//  "Coin": "Bitcoin", 
//  "url": "/currencies/bitcoin/", 
//  "Symbol": "BTC", 
//  "Price": "$6122.67" 
// } 

私が間違っていることは何ですか?

返信用Thx!

+0

。あなたがそれをあなたのブラウザコンソールでテストし、それを正しく得るまでそれを調整することができます定期的にそれをこすりなさい。 – pguardiario

答えて

1

あなたは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 
})) 
関連する問題