-
Notifications
You must be signed in to change notification settings - Fork 1
/
sat_down.py
89 lines (71 loc) · 1.94 KB
/
sat_down.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
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
"""
这个文件是一个下载接口,
"""
import urllib
import urllib.request
import time
import datetime
"""
global var
"""
debug = False
#debug = True
if __name__ == '__main__':
debug = True
else:
debug = False
"""
公共下载接口
"""
import qt_down
import sn_down
import yh_down
def Down(code):
return qt_down.QtDown(code)
def DownMulti(codes):
return qt_down.QtDownMulti(codes)
def DownHistorySN(code, dtEnd, dtStart, f):
sn_down.SinaDownHistory(code, dtEnd, dtStart, f)
def DownHistoryYH(code, dtEnd, dtStart, f):
yh_down.YahooDownHistory(code, dtEnd, dtStart, f)
"""
获取实时数据,这个可以优化文件句柄
不是每一个链接都创建一个文件句柄,可以使用文件句柄池,不用每次都打开
"""
def GetRtPriceByURL(url):
try:
f= urllib.request.urlopen(url)
if(debug): print(f.geturl())
if(debug): print(f.info())
return f.readline()
f.close()
except IOError:
print("url open failed", url)
return ''
#最好的方式是调用方控制readlines()的大小,保证一次就能返回所有的数据。
#如果超出大小,该函数会提示错误,程序退出
def GetMultiRtPriceByURL(url):
try:
f= urllib.request.urlopen(url)
if(debug): print(f.geturl())
if(debug): print(f.info())
return f.readlines()
f.close()
except IOError:
print("url open failed ....", url)
return None
def Test():
codes = ['sh601398']
url = 'http://qt.gtimg.cn/q='+codes[0]
print(GetRtPriceByURL(url))
def TeestMulti():
codes=['000001','000002']
print(DownMulti(codes))
if __name__ == '__main__':
start = time.time()
Test()
#TestMulti()
end = time.time()
print("url open second %d" %(end - start))