使用tushare库
获取股票信息
1.安装tushare库
win+R输入cmd进入控制台
输入pip install tushare
2.获取股票信息
1 import tushare,time2 #导入tushare库3 data = tushare.get_realtime_quotes(‘000581’)4 #获取股票代码为000581的股票信息5 print(data)
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库详细用法
参考资料:
完整代码-------------------------------------------------------------------
1 import tushare,time 2 3 def getrealtimedata(share): 4 data = tushare.get_realtime_quotes(share.code) 5 share.name = data.loc[0][0] 6 share.open = float(data.loc[0][1]) 7 share.price = float(data.loc[0][3]) 8 share.high = float(data.loc[0][4]) 9 share.low = float(data.loc[0][5])10 share.describe='股票编号:{},股票名称:{},今日开盘价:{},当前价格:{},今日最高价:{},今日最低价:{}'.format(share.code,share.name,share.open,share.price,share.high,share.low)11 return share12 13 class Share():14 def __init__(self,code,buy,sale):15 self.name = ''16 self.open = ''17 self.price = ''18 self.high = ''19 self.low = ''20 self.describe=''21 self.code = code22 self.buy = buy23 self.sale = sale24 25 def main(sharelist):26 # share = Share(code)27 for share in sharelist:28 sss=getrealtimedata(share)29 print(sss.describe)30 31 if sss.price <=sss.buy:32 print('价格超低,赶紧买入!')33 elif sss.price >= sss.sale:34 print('赶紧卖出。大赚了!')35 else:36 print('静观其变……')37 38 while True:39 share1=Share("000581",18.7,19.0)40 share2=Share("600106",18.7,19.0)41 share3=Share("000591",18.7,19.0)42 sharelist = [share1,share2,share3]43 main(sharelist)44 time.sleep(5)