参考:
https://www.cnblogs.com/hbhjkzhangmin/p/10869504.html
安装tushare
pip install tushare
参考: http://tushare.org/
python基础篇—监控股票
使用tushare库
获取股票信息
1.安装tushare库
win+R输入cmd进入控制台
输入pip install tushare
2.获取股票信息
import tushare,time
#导入tushare库
data = tushare.get_realtime_quotes('000581')
#获取股票代码为000581的股票信息
print(data)
log 为:
D:\PYTHON_ENV\stock\Scripts\python.exe C:/Users/xxxx/PycharmProjects/stock/test.py
name open pre_close price ... a5_p date time code
0 威孚高科 20.070 20.000 20.320 ... 20.380 2020-01-16 11:07:24 000581
[1 rows x 33 columns]
Process finished with exit code 0
3.根据自定义要求,获取指定数据
因为tushare获取的返回值是一个pandas类型数据。可以用loc方式获取单独数据
data = tushare.get_realtime_quotes(share.code)
share.name = data.loc[0][0]
share.open = float(data.loc[0][1])
share.price = float(data.loc[0][3])
share.high = float(data.loc[0][4])
share.low = float(data.loc[0][5])
4.tushare库详细用法
参考资料:http://tushare.waditu.com/trading.html
完整代码-------------------------------------------------------------------
import tushare,time
def getrealtimedata(share):
data = tushare.get_realtime_quotes(share.code)
share.name = data.loc[0][0]
share.open = float(data.loc[0][1])
share.price = float(data.loc[0][3])
share.high = float(data.loc[0][4])
share.low = float(data.loc[0][5])
share.describe='股票编号:{},股票名称:{},今日开盘价:{},当前价格:{},今日最高价:{},今日最低价:{}'.format(share.code,share.name,share.open,share.price,share.high,share.low)
return share
class Share():
def __init__(self,code,buy,sale):
self.name = ''
self.open = ''
self.price = ''
self.high = ''
self.low = ''
self.describe=''
self.code = code
self.buy = buy
self.sale = sale
def main(sharelist):
# share = Share(code)
for share in sharelist:
sss=getrealtimedata(share)
print(sss.describe)
if sss.price <=sss.buy:
print('价格超低,赶紧买入!')
elif sss.price >= sss.sale:
print('赶紧卖出。大赚了!')
else:
print('静观其变……')
while True:
share1=Share("000581",18.7,19.0)
share2=Share("600106",18.7,19.0)
share3=Share("000591",18.7,19.0)
sharelist = [share1,share2,share3]
main(sharelist)
time.sleep(5)
来源:CSDN
作者:wowocpp
链接:https://blog.csdn.net/wowocpp/article/details/104001350