-
Notifications
You must be signed in to change notification settings - Fork 24
/
dataqoute.py
291 lines (258 loc) · 12.3 KB
/
dataqoute.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# coding:utf8
from re import split
from pyecharts.charts.basic_charts.kline import Kline
import requests
from requests.api import head, request
import json
import datetime
import re
'''
日:
kline_dayqfq={"code":0,"msg":"","data":{"hk01082":{"qfqday":[["2019-09-03","0.810","0.820","0.820","0.810","304000.000",{},"0.000","24.628"],["2019-09-04","0.780","0.790","0.790","0.780","1400000.000",{},"0.000","109.208"],["2019-09-05","0.760","0.770","0.780","0.760","472000.000",{},"0.000","36.276"],
https://web.ifzq.gtimg.cn/appstock/app/hkfqkline/get?_var=kline_dayqfq¶m=hk01082,day,,,320,qfq&r=0.740469290856042
周:
kline_weekqfq={"code":0,"msg":"","data":{"hk01082":{"qfqweek":[["2014-10-17","1.586","1.464","1.586","1.450","520726000.000",{},"0.000","9857.681"],["2014-10-24","1.468","1.361","1.486","1.336","183536000.000",{},"0.000","2871.940"],["2014-10-31","1.361","1.364","1.418","1.325","280686000.000",{},"0.000","4111.772"],["2014-11-07","1.350","1.371","1.407","1.318","108458000.000",{},"0.000","1559.885"],["2014-11-14","1.371","1.375","1.411","1.346","122104000.000",
https://web.ifzq.gtimg.cn/appstock/app/hkfqkline/get?_var=kline_weekqfq¶m=hk01082,week,,,320,qfq&r=0.005481024716898597
月:
kline_monthqfq={"code":0,"msg":"","data":{"hk01082":{"qfqmonth":[["2011-07-29","6.754","5.539","6.861","5.396","118660051.000",{},"0.000","16862.541"],["2011-08-31","5.539","4.325","5.539","4.075","13322700.000",{},"0.000","1547.295"],["2011-09-30","4.325","3.539","4.325","3.075","1244000.000",{},"0.000","100.408"],
https://web.ifzq.gtimg.cn/appstock/app/hkfqkline/get?_var=kline_monthqfq¶m=hk01082,month,,,320,qfq&r=0.6635249910650687
实时:
v_r_hk01082="100~香港教育国际~01082~1.300~1.300~0.000~0.0~0~0~1.300~0~0~0~0~0~0~0~0~0~1.300~0~0~0~0~0~0~0~0~0~0.0~2020/12/16 09:20:17~0.000~0.00~1.300~1.300~1.300~0.0~0.000~0~-7.05~~0~0~0.00~7.5539~7.5539~HK EDU INTL~0.00~1.390~0.610~0.00~39.29~0~0~0~0~0~-7.05~7.00~0.00~4000~62.50~~GP~-12.82~-9.19";
https://web.sqt.gtimg.cn/q=r_hk01082?r=0.07942820340450552
'''
class DataQoute:
def __init__(self):
#实时价格
'''
http://hq.sinajs.cn/list=sh601006
'''
self.time_data_api = 'http://hq.sinajs.cn/list='
'''
最近二十天左右的每5分钟数据
http://money.finance.sina.com.cn/quotes_service/api/json_v2.php/CN_MarketData.getKLineData?symbol=sz000001&scale=5&ma=5&datalen=1023
(参数:股票编号、分钟间隔(5、15、30、60、240、1200)、均值(5、10、15、20、25)、查询个数点(最大值242))
'''
#历史数据
self.mink_data_api= 'http://money.finance.sina.com.cn/quotes_service/api/json_v2.php/CN_MarketData.getKLineData?'
self.hk_day_api='https://web.ifzq.gtimg.cn/appstock/app/hkfqkline/get?_var=kline_dayqfq¶m=%s,day,,,1000,qfq&r=0.740469290856042'
self.hk_week_api='https://web.ifzq.gtimg.cn/appstock/app/hkfqkline/get?_var=kline_weekqfq¶m=%s,week,,,1000,qfq&r=0.005481024716898597'
self.hk_month_api ='https://web.ifzq.gtimg.cn/appstock/app/hkfqkline/get?_var=kline_monthqfq¶m=%s,month,,,320,qfq&r=0.6635249910650687'
self.hk_codes_api ='http://app.finance.ifeng.com/hq/list.php?type=hkstock'
self.a_shcodes_api ='http://app.finance.ifeng.com/hq/list.php?type=stock_a&class=ha'
self.a_szcodes_api='http://app.finance.ifeng.com/hq/list.php?type=stock_a&class=sa'
self.a_gemcodes_api='http://app.finance.ifeng.com/hq/list.php?type=stock_a&class=gem'
self.header = {
'Accept-Encoding':
'gzip, deflate, sdch',
'User-Agent':
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.100 Safari/537.36'
}
self.code =''
self.name =""
self.hk_codes={}
self.sh_codes={}
self.sz_codes={}
self.gem_codes={}
self.session = requests.session()
def SetCode(self,code):
self.code=code
def GetTimeData(self):
request_url = self.time_data_api +self.code
time_data={}
try:
r = self.session.get(request_url,headers = self.header)
#sh601006=大秦铁路,6.600,6.600,6.550,6.610,6.530,6.540,6.550,16365460,107231427.000,214838,6.540,859590,6.530,689800,6.520,10359
splits=r.text.split("=")[1].replace('"','').split(",")
self.name=splits[0]
time_data=dict(
code=self.code,
name=splits[0],
open=float(splits[1]),
close=float(splits[2]),
now=float(splits[3]),
high=float(splits[4]),
low=float(splits[5]),
buy=float(splits[6]),
sell=float(splits[7]),
turnover=int(splits[8]),
volume=float(splits[9]),
bid1_volume=int(splits[10]),
bid1=float(splits[11]),
bid2_volume=int(splits[12]),
bid2=float(splits[13]),
bid3_volume=int(splits[14]),
bid3=float(splits[15]),
bid4_volume=int(splits[16]),
bid4=float(splits[17]),
bid5_volume=int(splits[18]),
bid5=float(splits[19]),
ask1_volume=int(splits[20]),
ask1=float(splits[21]),
ask2_volume=int(splits[22]),
ask2=float(splits[23]),
ask3_volume=int(splits[24]),
ask3=float(splits[25]),
ask4_volume=int(splits[26]),
ask4=float(splits[27]),
ask5_volume=int(splits[28]),
ask5=float(splits[29]),
date=splits[30],
time=splits[31],
)
except Exception as e:
print(self.code,str(Exception),e)
return time_data
'''
5
15
30
60
240 日
1200 周
'''
def GetKData(self,freq):
param = 'symbol=%s&scale=%s&ma=5&datalen=1000'%(self.code,freq)
time_fomart ='%Y-%m-%d %H:%M:%S'
if freq =='240' or freq == '1200':
time_fomart='%Y-%m-%d'
request_url = self.mink_data_api+param
klines=[]
try:
r=self.session.get(request_url,headers=self.header)
klines =json.loads(r.text)
for i in range(len(klines)):
klines[i]["date"]=klines[i]["day"] #datetime.datetime.strptime(klines[i]["day"],time_fomart)
klines[i]['code']=self.code
del klines[i]["day"]
except Exception as e:
print(self.code,str(Exception),str(e))
return klines
'''
day 日
week 周
month 月
'''
def GetHkKData(self,freq):
request_url = ""
data_key=""
if freq=="day":
request_url=self.hk_day_api%(self.code)
data_key="qfqday"
elif freq=="week":
request_url = self.hk_week_api%(self.code)
data_key="qfqweek"
elif freq=="month":
request_url = self.hk_month_api%(self.code)
data_key='qfqmonth'
else:
print("error,freq is error")
return []
klines =[]
try:
r=self.session.get(request_url,headers = self.header)
r.encoding="utf-8"
data_text = r.text.split("=")[1]
data=json.loads(data_text)
kdatas = data['data'][self.code][data_key] if data['data'][self.code].__contains__(data_key) else data['data'][self.code][freq]
'''
['2020-12-16', '7.700', '7.770', '7.830', '7.580', '142194865.00', {}, '0', '110189.61']
'''
for i in range (len(kdatas)):
current_kdata= kdatas[i]
kline={}
kline['date']= current_kdata[0]
kline['code']=self.code
kline['open']=float(current_kdata[1])
kline['close']=float(current_kdata[2])
kline['high']=float(current_kdata[3])
kline['low']=float(current_kdata[4])
kline['amount']=float(current_kdata[5])
kline['volume']=float(current_kdata[8])
klines.append(kline)
except Exception as e:
print (self.code,str(Exception),str(e))
return klines
def GetHkCodes(self):
r=self.session.get(self.hk_codes_api,headers=self.header)
#<li><a href="http://finance.ifeng.com/app/hq/hkstock/hk
#<li><a href="http://finance.ifeng.com/app/hq/hkstock/hk1361/index.shtml" target="_blank">361度(1361)</a></li>
s='<li><a href="http://finance.ifeng.com/app/hq/hkstock/hk([0-9]*)/index.shtml" target="_blank">(.*)\([0-9]*\)</a></li>'
html = r.text
pat = re.compile(s)
codes= pat.findall(html)
for code in codes:
hk_code={}
hk_code['code']= 'hk'+code[0]
hk_code['name']=code[1]
self.hk_codes['hk'+code[0]]=hk_code
str_codes =json.dumps(self.hk_codes,ensure_ascii=False)
file=open("hkcodes.txt","w")
file.write(str_codes)
file.close()
def GetShCodes(self):
r=self.session.get(self.a_shcodes_api,headers=self.header)
#<li><a href="http://finance.ifeng.com/app/hq/hkstock/hk
#<li><a href="http://finance.ifeng.com/app/hq/hkstock/hk1361/index.shtml" target="_blank">361度(1361)</a></li>
s='<li><a href="http://finance.ifeng.com/app/hq/stock/sh([0-9]*)/index.shtml" target="_blank">(.*)\([0-9]*\)</a></li>'
html = r.text
pat = re.compile(s)
codes= pat.findall(html)
for code in codes:
sh_code={}
sh_code['code']= 'sh'+code[0]
sh_code['name']=code[1]
self.sh_codes['sh'+code[0]]=sh_code
str_codes =json.dumps(self.sh_codes,ensure_ascii=False)
file=open("shcodes.txt","w")
file.write(str_codes)
file.close()
def GetSzCodes(self):
r=self.session.get(self.a_szcodes_api,headers=self.header)
#<li><a href="http://finance.ifeng.com/app/hq/hkstock/hk
#<li><a href="http://finance.ifeng.com/app/hq/hkstock/hk1361/index.shtml" target="_blank">361度(1361)</a></li>
s='<li><a href="http://finance.ifeng.com/app/hq/stock/sz([0-9]*)/index.shtml" target="_blank">(.*)\([0-9]*\)</a></li>'
html = r.text
pat = re.compile(s)
codes= pat.findall(html)
for code in codes:
sz_code={}
sz_code['code']= 'sz'+code[0]
sz_code['name']=code[1]
self.sz_codes['sz'+code[0]]=sz_code
str_codes =json.dumps(self.sz_codes,ensure_ascii=False)
file=open("szcodes.txt","w")
file.write(str_codes)
file.close()
def GetGemCodes(self):
r=self.session.get(self.a_gemcodes_api,headers=self.header)
#<li><a href="http://finance.ifeng.com/app/hq/hkstock/hk
#<li><a href="http://finance.ifeng.com/app/hq/hkstock/hk1361/index.shtml" target="_blank">361度(1361)</a></li>
s='<li><a href="http://finance.ifeng.com/app/hq/stock/sz([0-9]*)/index.shtml" target="_blank">(.*)\([0-9]*\)</a></li>'
html = r.text
pat = re.compile(s)
codes= pat.findall(html)
for code in codes:
sz_code={}
sz_code['code']= 'sz'+code[0]
sz_code['name']=code[1]
self.gem_codes['sz'+code[0]]=sz_code
str_codes =json.dumps(self.gem_codes,ensure_ascii=False)
file=open("gemcodes.txt","w")
file.write(str_codes)
file.close()
if __name__ == "__main__":
code ='hk01093'
data_qoute=DataQoute()
data_qoute.SetCode('hk00272')
data_qoute.GetHkKData("day")
'''
data_qoute.GetHkCodes()
data_qoute.GetShCodes()
data_qoute.GetSzCodes()
data_qoute.GetGemCodes()
data_qoute.SetCode(code)
klines=data_qoute.GetHkKData("day")
print(len(klines))
print('==========================')
data_qoute.GetKData("240")
'''