-
Notifications
You must be signed in to change notification settings - Fork 0
/
sinahq.py
58 lines (53 loc) · 1.68 KB
/
sinahq.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
import http.client
import time
def getsinahq(codes):
try:
strcode = ''
for code in codes:
strcode += code + ','
conn = http.client.HTTPSConnection('hq.sinajs.cn', timeout=5)
conn.request(method='GET', url='/?list=' + strcode)
res = conn.getresponse()
str = res.read().decode('gb18030')
hqs = str.split(';\n')
hqlist = []
for hq in hqs:
l1 = hq.split('=')
if len(l1) < 2:
break
code = l1[0][-6:]
l2 = l1[1].split(',')
d = {}
d['code'] = code
d['name'] = l2[0]
d['open'] = float(l2[1])
d['last'] = float(l2[2])
d['close'] = float(l2[3])
d['high'] = float(l2[4])
d['low'] = float(l2[5])
hqlist.append(d)
return hqlist
except Exception as err:
print(err)
finally:
conn.close()
return None
'''
if __name__ == '__main__':
print(getsinahq(['sh600549','sh000001']))
'''
if __name__ == '__main__':
codes = ['sz002531', 'sz300418']
while True:
hqlist = getsinahq(codes)
if hqlist is None:
continue
for hq in hqlist:
zf = str(round(100 * (hq['close'] - hq['last']) / hq['last'], 2))
if hq['close'] >= hq['last']:
print('\033[31m' + hq['name'][1:2] + str(round(hq['close'], 2)) + '\033[0m')
print('\033[31m' + ' ' + zf + '\033[0m')
else:
print('\033[40m' + hq['name'][1:2] + str(round(hq['close'], 2)) + '\033[0m')
print('\033[40m' + ' ' + zf + '\033[0m')
time.sleep(3)