很久没用python了,出于学习跟兴趣结合,写了个看股票的脚本学习下。
首先安装python。
安装完以后你需要了解Tushare -财经数据接口包 网址:http://tushare.org/ 里面提供了各种你需要的方法,应有尽有。我们只需要调用实时股票价格就行,然后我没找到涨跌幅百分比,于是自己计算了一下。
安装完python后,引入Tushare之前需要先安装
pip install tushare
pip install pandas
执行pip命令要cmd要切换到安装目录下有个script的文件夹下面,里面有个pip.exe可执行文件
#encoding:utf-8
import tushare as ts
import time
#上市公司代码
codes = ['sh', 'sz', 'cyb', '300059','000776','512880','000792']
#codes = ['sh', 'sz', 'cyb', '300059', '002594','601633']
while 1:
data = ts.get_realtime_quotes(codes)
#print(data)
# print(len(data))
sh_price = round(float(data['price'].values[0]), 2)
sh_pre_close = round(float(data['pre_close'].values[0]), 2)
sz_price = round(float(data['price'].values[1]), 2)
sz_pre_close = round(float(data['pre_close'].values[1]), 2)
cyb_price = round(float(data['price'].values[2]), 2)
cyb_pre_close = round(float(data['pre_close'].values[2]), 2)
sh = round(sh_price - sh_pre_close, 2)
sz = round(sz_price - sz_pre_close, 2)
cyb = round(cyb_price - cyb_pre_close, 2)
sh_percent = round(sh / sh_pre_close * 100, 2)
sz_percent = round(sz / sz_pre_close * 100, 2)
cyb_percent = round(cyb / cyb_pre_close * 100, 2)
# print('(', sh_price, sh, sh_percent, ')', '(', sz_price, sz, sz_percent, ')', '(', cyb_price, cyb, cyb_percent, ')')
print('(%.2f %.2f %.2f) (%.2f %.2f %.2f) (%.2f %.2f %.2f)'%(sh_price, sh, sh_percent, sz_price, sz, sz_percent, cyb_price, cyb, cyb_percent))
for i in range(3, len(data)):
price = float(data['price'].values[i])
pre_close = float(data['pre_close'].values[i])
print((data.name.values[i]), data.open.values[i], data.price.values[i], round((price-pre_close)/pre_close*100, 2))
#请求间隔时间
time.sleep(3)
最后执行你的脚本 python zy.py
我的python版本是三点多的,二点多的会有乱码现象,建议更新