Python之tushare财经数据接口包案例。

tushare是一个免费开源的财经数据接口包,通过tushare这个模块,可以实现对采集、存储股票等金融数据,能够为金融分析人员提供快速、整洁、和多样的便于分析的数据。

案例说明:通过获取股票的实时数据,取出股票代码对应的信息以及股票数据状态对比,发送邮箱提醒。

案例代码:

import tushare
import time
import smtplib
from email.mime.text import MIMEText


class StockClass():
    '股票类'

    def __init__(self, stockCode, buyPoint, SellingPoint):
        self.stockName = ''
        self.price = ''
        self.highPrice = ''
        self.lowPrice = ''
        self.StockTime = ''
        self.StockCode = stockCode
        self.buyPoint = buyPoint
        self.SellingPoint = SellingPoint


def getStockData(StockClass):
    '获取股票数据'
    stockData = tushare.get_realtime_quotes(StockClass.StockCode)  # 获取股票的实时数据。
    # stockCod=tushare.get_realtime_quotes('000519')#获取股票的实时数据。
    # print(str(stockData))

    StockClass.stockName = stockData.loc[0][0]  # 股票名
    StockClass.stockCode = stockData.loc[0][-1]  # 股票代码
    StockClass.price = float(stockData.loc[0][3])  # 股票价格
    StockClass.highPrice = stockData.loc[0][4]  # 最高价
    StockClass.lowPrice = stockData.loc[0][5]  # 最低价
    StockClass.StockTime = stockData.loc[0][-2]  # 日期
    StockClass.getContent = '股票名:' + StockClass.stockName \
                            + '\n当前价格:' + str(StockClass.price) + '\n最高价格:' \
                            + str(StockClass.highPrice) + '\n时间:' + str(StockClass.StockTime)
    # print('股票名:',stockClass.stockName,'\n当前价格:',stockClass.price,
    #       '\n最高价格:',stockClass.highPrice,'\n时间:',stockClass.StockTime)
    # print(StockClass.getContent)
    return StockClass


def SendEmail(msgSubject, msgContent):
    '''
    发送邮件提醒
    :param msgSubject:邮件主题
    :param msgContent:邮件内容
    :return:邮件
    '''
    msgFrom = '[email protected]'
    msgPassWord = 'XXXXX'  #发送方

    msgT0 = '[email protected]'  # 接收方

    msgEmail = MIMEText(msgContent, 'utf-8')  # 构造邮件
    msgEmail['msgSubject'] = msgSubject
    msgEmail['msgContent'] = msgContent
    msgEmail['msgT0'] = msgT0

    try:
        ss = smtplib.SMTP('smtp.qq.com', 587)
        ss.ehlo()
        ss.starttls()  # 启动安全传输模式。
        ss.login(msgFrom, msgPassWord)
        ss.sendmail(msgFrom, msgT0, msgEmail.as_string())  # 发送邮件
        print('邮件发送成功..')
        ss.quit()
    except Exception as e:
        print('邮件发送失败:', e)


def main(ShareList):
    for i in ShareList:
        sss = getStockData(i)
        # print(sss.getContent)

    if sss.price <= sss.buyPoint:
        # print('\033[32m'+'%s达到买点,当前价格为%s。'%(sss.stockName,sss.price)+'\033[0m')
        SendEmail('达到买点', sss.getContent)
    elif sss.price >= sss.SellingPoint:
        # print('\033[31m'+'%s达到卖点,当前价格为%s。'%(sss.stockName,sss.price)+'\033[0m')
        SendEmail('达到卖点', sss.getContent)
    else:
        print('再等等。')


while True:
    i_1 = StockClass('600106', 3.1, 3.18)
    # i_2=StockClass('601988',3.4,3.5)
    # i_3=StockClass('000591',3.0,3.7)

    # list=[i_1,i_2,i_3]
    list = [i_1]
    # print('-----------')
    main(list)

    time.sleep(600)

猜你喜欢

转载自blog.csdn.net/qq_39979646/article/details/104479041