2017-10-30 5 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 
}))