python实时自动获取基金数据发送钉钉

发布时间:2024年01月18日
脚本的由来
  • 写这个脚本主要原因是在平时的工作当中,需要处理工作问题很忙时,没时间看基金情况,通过设置涨幅度值,从而快速确认是否需要卖出或者买入基金。
运行脚本所需环境
  • 需要安装dingtalkchatbot、requests 使用pip安装即可
  • 需要钉钉群组种创建机器人用于发送信息
  • 在写个定时任务周一到周五14:30执行一次 30 14 * * 1,2,3,4,5 python3 /jijin/demo.py
脚本详情
#!/usr/bin/python3
#coding:utf8
"""
定时推送购买基金涨幅情况,来却认是否赎购基金
基金实时信息:http://fundgz.1234567.com.cn/js/001186.js?rt=1463558676006
新浪基金实时信息: http://hq.sinajs.cn/list=s_sh000905
钉钉告警地址:https://oapi.dingtalk.com/robot/send?access_token=xxxxx
Data:2020年3月20日10:00:56
编写人: 赵路

1、新增大盘指数告警
"""
import requests,json,time
from dingtalkchatbot.chatbot import DingtalkChatbot

#获取时间戳
def GetTime():
    t = time.time()
    Timestamp = round(t * 1000)
    # print(Timestamp)
    return Timestamp

#传入大盘指数新浪接口
def MarketIndex(SharesID):
    MarketList = []
    Headers = {'content-type':'application/json','User-Agent': 'Apache-HttpClient/4.5.2 (Java/1.8.0_102)'}
    sinajs = "http://hq.sinajs.cn/list=s_" + str(SharesID)
    Msg = requests.get(sinajs, headers=Headers)
    SharesMsg =  Msg.text
    # print(SharesMsg)
    if SharesID in SharesMsg:
        GetSharesName = SharesMsg.split(",")[0].split("=")[1]
        Gain = SharesMsg.split(",")[3]
        GetMarketMsg = "大盘指数名称: " + GetSharesName  + " 涨跌幅: " +  str(Gain)
    return GetMarketMsg


#传入基金代码和当天时间戳获取当前数据
def GetFundData(fundId,GetTimestamp):
    Headers = {'content-type':'application/json','User-Agent': 'Apache-HttpClient/4.5.2 (Java/1.8.0_102)'}
    TTurl = "http://fundgz.1234567.com.cn/js/" + str(fundId) +  ".js?rt=" + str(GetTimestamp)
    r = requests.get(TTurl, headers=Headers)
    GetMsg = r.text
    # print(GetMsg)
    if "fundcode" in GetMsg:
        FundID = GetMsg.split(",")[0].split(":")[1]
        FundName = GetMsg.split(",")[1].split(":")[1]
        Gain = GetMsg.split(",")[5].split(":")[1]
        # print(GetMsg.split(",")[6].split("}")[0])
        currentTime = GetMsg.split(",")[6].split("}")[0].split("\"gztime\":")[1]
        # GetData = "基金名称:"+ FundName + "ID" + FundID + "涨跌幅:" + Gain + "当前时间:" + currentTime
        # GetData =  FundName + " " + FundID + " " + Gain
        # GetData = "基金名称:"+ FundName + "涨跌幅:" + Gain + "当前时间:" + currentTime
        GetData = "基金名称:"+ FundName + " "+ "涨跌幅:" + Gain + " "+ "当前时间:" + currentTime
    return GetData

#判断涨幅度正负1.5个点的输出基金
def ExeIncreaseDegree(Fundmsg,Differencevalue,Increment):
    if Fundmsg:
        IncreaseDegree = float(Fundmsg.split(":")[2].split("当前时间")[0].replace("\"",''))
        if  IncreaseDegree  > Increment  or IncreaseDegree < Differencevalue:
            # DifferencevalueincrementList.append(Fundmsg)
            return Fundmsg
    # print(DifferencevalueincrementList)


#钉钉告警通知
def SendMsg(Title,Msg):
    # WebHook地址
    webhook = "https://oapi.dingtalk.com/robot/send?access_token=(自行申请)"
    # 初始化机器人小丁
    xiaoCCOD = DingtalkChatbot(webhook)
    # Text消息@所有人,添加jijin_send关键字否则无法告警
    xiaoCCOD.send_text(msg="jijin_send---> " + Title + "\n" + Msg, is_at_all=True)


if __name__ ==  '__main__':
    #持有基金ID
    #fundIdList = ["001631","501021"]
	fundIdList = ["001549","001594"]
    TitleList = ["每日基金涨跌详情","当日基金涨跌正负1.5详情","大盘指数详情"]
    #大盘指数列表
    SharesIDList = ["sh000001","sh000300","sh000905"]
    GetSharesMsg = []
    GetTimestamp = GetTime()
    MsgList = []
    DifferencevalueincrementList = []
    Differencevalue = -1.5
    Increment = 1.5

    #循环大盘ID发送告警
    for SharesID in SharesIDList:
        GetMarketIndexMsg =  MarketIndex(SharesID)
        GetSharesMsg.append(GetMarketIndexMsg)
    SendSharesMsg  =  str(GetSharesMsg).split("[")[1].split("]")[0].replace("None, ", "").replace(",", "\n")
    SendMsg(TitleList[2], SendSharesMsg)

    #循环基金ID获取基金当前涨幅度
    for fundId in fundIdList:
        GetData = GetFundData(fundId,GetTimestamp)
        Fundmsg =  GetData
        DifferencevalueincrementList.append(ExeIncreaseDegree(Fundmsg,Differencevalue,Increment))
        MsgList.append(GetData)

    #正负情况大于1.5发送信息
    GetNewData = str(DifferencevalueincrementList).split("[")[1].split("]")[0].replace("None, ","").replace(",","\n")
    if None is not GetNewData:
        SendMsg(TitleList[1], GetNewData)

    #去重大于1.5的基金信息之后在发送信息
    DifferenceSet = [item for item in MsgList if item not in DifferencevalueincrementList]
    GetDifferencevalueincrement = str(DifferenceSet).split("[")[1].split("]")[0].replace("None, ","").replace('\"','').replace(", ","\n")
    SendMsg(TitleList[0], GetDifferencevalueincrement)
执行结果
  • 运行结果

本文由mdnice多平台发布

文章来源:https://blog.csdn.net/zhao138969/article/details/135678172
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。