훈훈훈

파이썬(Python) :: 암호 화폐 과거 데이터 크롤링하기 본문

파이썬/Python

파이썬(Python) :: 암호 화폐 과거 데이터 크롤링하기

훈훈훈 2020. 3. 9. 21:08

# 업비트 Open API 이용

- Example URL :

https://crix-api-endpoint.upbit.com/v1/crix/candles/기간단위/기간?code=CRIX.UPBIT.마켓-암호화폐기호&count=데이터수&to=최종시세데이터일시

 

# 스크랩핑 코인 종류

- BTC, ETH, BSV 등등

 

# 과거 데이터 크롤링 

- 1분, 3분 5분, 1일, 1주 단위 등등

 

# Pandas 활용하여 csv 파일로 output 

import requests
import pandas as pd

from bs4 import BeautifulSoup

coin_list       = ['BTC', 'ETH', 'BSV', 'XRP', 'BCH', 'EOS'] 
time_units      = ['days', 'weeks']
minutes_units   = [1, 3, 5, 15, 30, 60, 240] 

'''
'Time'                 : 시간
'OpeningPrice'         : 시가
'HighPrice'            : 고가
'LowPrice'             : 저가
'TradePrice'           : 체결가
'CandleAccTradeVolume' : 누적 거래량
'CandleAccTradePrice'  : 누적 체결가
'''

for coin in coin_list:
    
    for time_unit in time_units:
        '''
        Scraping days & weeks 
        '''
        req    = requests.get(f'https://crix-api-endpoint.upbit.com/v1/crix/candles/{time_unit}?code=CRIX.UPBIT.KRW-{coin}&count=100&')
        data   = req.json()
        result = []

        for i, candle in enumerate(data):
            result.append({
                'Time'                 : data[i]["candleDateTimeKst"], 
                'OpeningPrice'         : data[i]["openingPrice"],
                'HighPrice'            : data[i]["highPrice"],
                'LowPrice'             : data[i]["lowPrice"],
                'TradePrice'           : data[i]["tradePrice"],
                'CandleAccTradeVolume' : data[i]["candleAccTradeVolume"],
                "candleAccTradePrice"  : data[i]["candleAccTradePrice"]
            })
        coin_data = pd.DataFrame(result)
        coin_data.to_csv(f'{coin}_KRW_{time_unit}.csv')


    for minutes_unit in minutes_units:
        '''
        Scraping minutes data 
        '''
        req    = requests.get(f'https://crix-api-endpoint.upbit.com/v1/crix/candles/minutes/{minutes_unit}?code=CRIX.UPBIT.KRW-{coin}&count=400')
        data   = req.json()
        result = []

        for i, candle in enumerate(data):
            result.append({
                'Time'                 : data[i]["candleDateTimeKst"], 
                'OpeningPrice'         : data[i]["openingPrice"],
                'HighPrice'            : data[i]["highPrice"],
                'LowPrice'             : data[i]["lowPrice"],
                'TradePrice'           : data[i]["tradePrice"],
                'CandleAccTradeVolume' : data[i]["candleAccTradeVolume"],
                "candleAccTradePrice"  : data[i]["candleAccTradePrice"]
            })
        coin_data = pd.DataFrame(result)
        coin_data.to_csv(f'{coin}_KRW_{minutes_unit}min.csv')
Comments