mmyjh86

[PYSTOCK] Bithumb API 중에서 OHLCV 스크래핑

Aug 25th, 2019
416
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import requests
  2. import json
  3. from bs4 import BeautifulSoup
  4. import datetime
  5. from pandas import DataFrame
  6.  
  7. symbol = "BTC"
  8. interval = "day"
  9.  
  10. int2type = {
  11.     "day": "24H",
  12.     "hour12": "12H",
  13.     "hour6": "06H",
  14.     "hour": "01H",
  15.     "minute30": "30M",
  16.     "minute10": "10M",
  17.     "minute5": "05M",
  18.     "minute3": "03M",
  19. }
  20.  
  21. url = "https://m.bithumb.com/trade/chart/{}".format(symbol)
  22. resp = requests.get(url)
  23. html = resp.text
  24.  
  25. # parsing coin type
  26. string = html.split("COIN = ")[1].split(";")[0]
  27. coin = json.loads(string)
  28. tk2ct = {v['symbol_name']: k for k, v in coin['C0100'].items()}
  29.  
  30. # parsing xcoin name
  31. selector = "#barcodeForm > input[type=hidden]:nth-child(2)"
  32. soup = BeautifulSoup(html, 'html5lib')
  33. xcoin_name = soup.select(selector)[0]['value']
  34.  
  35. url = "https://m.bithumb.com/trade_history/chart_data"
  36. headers = {
  37.     "cookie": 'csrf_xcoin_name={}'.format(xcoin_name),
  38.     "x-requested-with": "XMLHttpRequest"
  39. }
  40. data = {
  41.     "coinType": tk2ct[symbol],
  42.     "crncCd": "C0100",
  43.     "tickType": int2type[interval],
  44.     "csrf_xcoin_name": xcoin_name
  45. }
  46.  
  47. resp = requests.post(url, data=data, headers=headers).json()
  48. for x in resp['data']:
  49.     x[0] = datetime.datetime.fromtimestamp(x[0] / 1000)
  50.  
  51. columns = [symbol, 'open', 'close', 'high', 'low', 'volume']
  52. df = DataFrame(resp['data'], columns=columns)
  53. df = df.set_index(symbol)
  54. print(df.head())
RAW Paste Data