侧边栏壁纸
博主头像
平凡的运维之路博主等级

行动起来,活在当下

  • 累计撰写 49 篇文章
  • 累计创建 25 个标签
  • 累计收到 3 条评论

目 录CONTENT

文章目录

python实时获取指定基金数据发送钉钉

平凡的运维之路
2024-07-16 / 1 评论 / 0 点赞 / 89 阅读 / 4834 字

脚本的由来

  • 写这个脚本主要原因是在平时的工作当中,需要处理工作问题很忙时,没时间看基金情况,通过设置涨幅度值,从而快速确认是否需要卖出或者买入基金

运行脚本所需环境

  • 需要安装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=acf237191e252cb24ca33d92dde7f5e45b37b82bb771b2aeb5d5d1ebf8addd222
Data:2020年3月20日10:00:56
编写人: Mr.zhao

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=acf237191e252cb24ca33d92dde71f5e45b37b8bb771b2aeb5d5d1ebf8addd222"
    # 初始化机器人小丁
    xiaoCCOD = DingtalkChatbot(webhook)
    # Text消息@所有人,添加CCOD关键字否则无法告警
    xiaoCCOD.send_text(msg="Mrzhao---> " + 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)

执行结果

  • 运行结果
    image.png
0

评论区