├── http_method ├── __init__.py ├── config.json └── monitor.py ├── .gitignore ├── readme.md └── main.py /http_method/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | __pycache__/ -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # BTC realtime tracking 2 | 3 | # 比特币实时行情监控 4 | 5 | * python3+ 6 | 7 | * http与socketio爬取两种方式 8 | 9 | * 公众号: 可转债量化分析 10 | 11 | [![skCVPJ.jpg](https://s3.ax1x.com/2021/01/05/skCVPJ.jpg)](https://imgchr.com/i/skCVPJ) -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | # -*- coding: UTF-8 -*- 2 | """ 3 | @author:xda 4 | @file:main.py 5 | @time:2021/01/08 6 | """ 7 | from http_method.monitor import BTCPrice 8 | def main(): 9 | btc = BTCPrice() 10 | btc.display() 11 | 12 | if __name__ == '__main__': 13 | main() 14 | -------------------------------------------------------------------------------- /http_method/config.json: -------------------------------------------------------------------------------- 1 | { 2 | "url": "https://api.mytokenapi.com/api/v1/index/currencycal?pair_id=1334945&period=4h&language=zh_cn×tamp=1610067196252&code=4f8d3c79f673691ad4fa233e3dbfe90d&platform=web_pc&v=1.0.0&legal_currency=CNY", 3 | "headers": { 4 | "accept": "application/json, text/plain, */*", 5 | "accept-encoding": "gzip, deflate, br", 6 | "accept-language": "zh,en;q=0.9,en-US;q=0.8,zh-CN;q=0.7", 7 | "cache-control": "no-cache", 8 | "content-type": "application/x-www-form-urlencoded", 9 | "origin": "https://www.mytokencap.com", 10 | "pragma": "no-cache", 11 | "referer": "https://www.mytokencap.com/", 12 | "sec-fetch-dest": "empty", 13 | "sec-fetch-mode": "cors", 14 | "sec-fetch-site": "cross-site", 15 | "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36" 16 | } 17 | } -------------------------------------------------------------------------------- /http_method/monitor.py: -------------------------------------------------------------------------------- 1 | # -*- coding: UTF-8 -*- 2 | """ 3 | @author:xda 4 | @file:monitor.py 5 | @time:2021/01/08 6 | """ 7 | import datetime 8 | 9 | import requests 10 | import json 11 | import os 12 | import time 13 | 14 | # update btc price every 10s 15 | INTERVAL = 10 16 | 17 | class BTCPrice(): 18 | 19 | def __init__(self): 20 | file = 'config.json' 21 | # print(os.getcwd()) 22 | path = os.path.join(self.path, file) 23 | self.config = self.read_config(path) 24 | 25 | @property 26 | def path(self): 27 | return os.path.dirname(os.path.abspath(__file__)) 28 | 29 | def get(self): 30 | resp = requests.get( 31 | url=self.config['url'], 32 | headers=self.config['headers']) 33 | if resp.status_code == 200: 34 | return resp.json() 35 | else: 36 | return [] 37 | 38 | @property 39 | def data(self): 40 | data = self.get() 41 | if data: 42 | return self.price(data) 43 | else: 44 | return None 45 | 46 | def display(self): 47 | while True: 48 | print(f'{datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")} : ',self.data) 49 | time.sleep(INTERVAL) 50 | 51 | def price(self,row): 52 | return row['status']['summery']['curr'] 53 | 54 | def read_config(self, file): 55 | with open(file, 'r') as f: 56 | return json.load(f) 57 | 58 | 59 | if __name__=='__main__': 60 | btc = BTCPrice() 61 | btc.display() --------------------------------------------------------------------------------