# coding=utf-8
import time
import httpConn
import dbConn
def getA000001():
url = 'http://push2his.eastmoney.com/api/qt/stock/trends2/get?secid=1.000001&ut=&fields1=f1%2Cf2%2Cf3%2Cf4%2Cf5%2Cf6%2Cf7%2Cf8%2Cf9%2Cf10%2Cf11&fields2=f51%2Cf53%2Cf56%2Cf58&iscr=0&ndays=1'
conn = httpConn.HttpConnect()
data = conn.request_get(url)
if data == '':
print('查询A000001失败')
else:
ts = data['data']['time']
at = time.strftime("%Y-%m-%d", time.localtime(ts))
dt = time.strftime("%Y-%m-%d", time.localtime())
if at != dt:
print('非交易日')
return False
else:
hour = int(time.strftime("%H", time.localtime()))
minute = int(time.strftime("%M", time.localtime()))
if (hour >= 15):
return True
else:
print('非交易时段')
return False
def getAllTodayPrice():
url = 'http://41.push2.eastmoney.com/api/qt/clist/get?pn=1&pz=5000&po=1&np=1&ut=&fltt=2&invt=2&fid=f3&fs=m:0+t:6,m:0+t:13,m:0+t:80,m:1+t:2,m:1+t:23&fields=f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f12,f13,f14,f15,f16,f17,f18,f20,f21,f23,f24,f25,f22,f11,f62,f128,f136,f115,f152'
conn = httpConn.HttpConnect()
data = conn.request_get(url)
# print(data)
if data == '':
print('查询code失败')
else:
db = dbConn.MY_SQL()
db.save_today_table(data['data']['diff'], time.strftime("%Y-%m-%d", time.localtime()))
def getSHSZDayLine(code):
url = 'http://28.push2his.eastmoney.com/api/qt/stock/kline/get?secid='+code+'&ut=&fields1=f1%2Cf2%2Cf3%2Cf4%2Cf5&fields2=f51%2Cf52%2Cf53%2Cf54%2Cf55%2Cf56%2Cf57%2Cf58&klt=101&fqt=0&end=20500101&lmt=10000'
conn = httpConn.HttpConnect()
res = conn.request_get(url)
print(code)
if res == '':
print('查询dayLine失败')
else:
data = res['data']
lines = []
preShouPan = 0
for i in data['klines']:
arr = i.split(',')
# 如果是上证000001,转换一下代码,以防和平安银行[000001]冲突
if data['code'] == '000001':
arr.append('100001')
else:
arr.append(data['code'])
arr.append(data['name'])
if data['market'] == 0:
arr.append('1')
else:
arr.append('0')
if preShouPan == 0:
arr.append(0)
else:
arr.append('%.3f' % ((float(arr[2]) - float(preShouPan))/float(preShouPan)*100))
preShouPan = float(arr[2])
# print(arr)
lines.append(arr)
db = dbConn.MY_SQL()
db.save_day_line_table(lines)
if getA000001():
s = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
startTime = time.time()
getAllTodayPrice()
getSHSZDayLine('1.000001')
getSHSZDayLine('0.399001')
print(s + ' 当日收盘数据用时: ' + str(time.time() - startTime))