forked from mpquant/Ashare
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuperLevel3.py
73 lines (55 loc) · 2.27 KB
/
superLevel3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import sys
import json
import requests
import datetime
import pandas as pd
import time
import sched
class SuperLevel(object):
stock_pool_hold = {'sh000001':'指数','sh601012':'隆基股份','sz300274':'阳光电源','sh600438':'通威股份','sh603098':'森特股份','sz002459':'晶澳股份','sz000762':'西藏矿业','hk01880':'中国中免','hk06610':'飞天云动', 'hk01810':'小米'}
def get_price(self):
now = datetime.datetime.now()
dataList = {}
timeStemp = now.strftime("%d/%m/%Y %H:%M:%S")
for code in self.stock_pool_hold:
URL = f'http://qt.gtimg.cn/q=s_{code}'
dstr = requests.get(URL).content.decode('gb2312')
key = dstr.split('=')[0].split("_")[2]
price = dstr.split('=')[1].replace('"','').split('~')[3]
priceArr = dataList[timeStemp] if (timeStemp in dataList) else {}
priceArr[key] = price
dataList[timeStemp] = priceArr
dataArr = []
for key in dataList:
data = {}
data['day'] = key
for item in dataList[key]:
itemKey = self.stock_pool_hold[item]
data[itemKey] = dataList[key][item]
dataArr.append(data)
dynamicColumns = []
dynamicColumns.append('day')
for code in self.stock_pool_hold:
dynamicColumns.append(self.stock_pool_hold[code])
pd.set_option('display.unicode.east_asian_width', True)
pd.set_option('display.unicode.east_asian_width', True)
df= pd.DataFrame(dataArr, columns = dynamicColumns)
df.day=pd.to_datetime(df.day);
df.set_index(['day'], inplace=True);
df.index.name=''
return df
def stockDetail(self):
df = self.get_price()
print('super-level-3:\n', df)
def time_printer(self):
now = datetime.datetime.now()
ts = now.strftime('%Y-%m-%d %H:%M:%S')
self.stockDetail()
self.loop_monitor()
def loop_monitor(self):
s = sched.scheduler(time.time, time.sleep)
s.enter(5, 1, self.time_printer, ())
s.run()
if __name__ == "__main__":
superLevel = SuperLevel()
superLevel.loop_monitor()