Python-简易的黄金价格监控+QQ邮箱提醒

目录

 实现功能

准备QQ邮箱授权码 

代码

效果展示

 实现功能

1、发送请求获取黄金信息

        ActivePrice:实时主动积存价格
        HighPrice:最高价
        LowPrice:最低价
        RegPrice:定期积存价
        SellPrice:赎回价

2、价格满足某一需求发送QQ邮箱提醒

准备QQ邮箱授权码 

登录QQ邮箱,点击设置,点击账号,然后下拉

POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务-去开启服务,获取授权码。(我这里已经获取过了所以是已开启状态)

代码

import time

import requests

"""
黄金监控
ActivePrice:实时主动积存价格
HighPrice:最高价
LowPrice:最低价
RegPrice:定期积存价
SellPrice:赎回价

邮箱授权码:twnexamsmegzeaie
"""


# 发送请求获取黄金积价格
def getGOLD():
    url = "https://mybank.icbc.com.cn/servlet/AsynGetDataServlet"

    querystring = {"tranCode": "A00622"}

    headers = {"content-type": "application/json"}

    response = requests.request("GET", url, headers=headers, params=querystring)

    """
    {'sysdate': '2024-04-19 15:42:19', 'TranErrorCode': '', 'rf': [{'goldTypeNo': 'JC001', 'RegPrice': '564.30', 'Active': '2', 'ProductName': '积存金', 'proCode': '080020000521', 'Reg': '1', 'HighPrice': '573.30', 'RegDate': '2024-04-19', 'ActivePrice': '565.90', 'SellPrice': '565.90', 'ActiveDate': '2024-04-19', 'LowPrice': '563.24'}], 'TranErrorDisplayMsg': ''}
    ActivePrice:实时主动积存价格
    HighPrice:最高价
    LowPrice:最低价
    RegPrice:定期积存价
    SellPrice:赎回价
    """
    # 获得JSON数据
    data = response.json()  # {'sysdate': '2024-04-19 19:05:59', 'TranErrorCode': '', 'rf': [{'goldTypeNo': 'JC001', 'RegPrice': '564.30', 'Active': '0', 'ProductName': '积存金', 'proCode': '080020000521', 'Reg': '1', 'HighPrice': '573.30', 'RegDate': '2024-04-19', 'ActivePrice': '563.43', 'SellPrice': '563.43', 'ActiveDate': '2024-04-19', 'LowPrice': '562.69'}], 'TranErrorDisplayMsg': ''}
    # 获取JSON中的rf信息
    ActivePrice = data[
        'rf']  # [{'goldTypeNo': 'JC001', 'RegPrice': '564.30', 'Active': '0', 'ProductName': '积存金', 'proCode': '080020000521', 'Reg': '1', 'HighPrice': '573.30', 'RegDate': '2024-04-19', 'ActivePrice': '563.43', 'SellPrice': '563.43', 'ActiveDate': '2024-04-19', 'LowPrice': '562.69'}]
    # ActivePrice[0] # {'goldTypeNo': 'JC001', 'RegPrice': '564.30', 'Active': '0', 'ProductName': '积存金', 'proCode': '080020000521', 'Reg': '1', 'HighPrice': '573.30', 'RegDate': '2024-04-19', 'ActivePrice': '563.43', 'SellPrice': '563.43', 'ActiveDate': '2024-04-19', 'LowPrice': '562.69'}

    return ActivePrice[0]['SellPrice']


# 价格超过发送QQ邮箱
def msg_qq(SellPrice):
    import base64
    import smtplib
    import sys
    from email.mime.text import MIMEText
    from email.header import Header

    # 邮箱内容
    mail_msg = f"""<h2>GOLD提醒<h2><p>当前黄金赎回价格:{str(SellPrice)}</p>"""
    message = MIMEText(mail_msg, 'html', 'utf-8')
    # 发件人名字,自由填写
    message["From"] = Header(f'=?utf-8?B?{base64.b64encode("Python炸天发送".encode()).decode()}=?= <26xxx43@qq.com>')
    # 收件人名字,自由填写
    message["To"] = Header('23xxx78@qq.com', 'utf-8')
    # 邮箱标题
    subject = 'Python发送GOD提醒,船长请接收'
    message["Subject"] = Header(subject, 'utf-8')

    # 发送方
    sender = '26xxx43@qq.com'
    # 接收方,可多个
    receivers = ['23xxx78@qq.com']

    # 使用QQ邮箱服务发送邮件
    try:
        password = "rdvcxxxxxic"  # QQ邮箱授权码
        smtpObj = smtplib.SMTP_SSL('smtp.qq.com', 465)  # 发件⼈邮箱中的SMTP服务器,端⼝是25
        # smtpObj.ehlo("zoubin") # 源码中获取我本机的完全限定域名 如电脑账户名叫‘张三’是中文直接报错UnicodeEncodeError: 'ascii'。如果电脑名称是中文需要这行代码
        smtpObj.login(sender, password)  # 登录SMTP服务器,括号中对应的是发件⼈邮箱账号、邮箱授权码
        smtpObj.sendmail(sender, receivers, message.as_string())  # 发送
        print("邮箱发送成功")
    except smtplib.SMTPException as e:
        print("Error: 无法发送邮件", e)
    finally:
        smtpObj.quit()  # 退出


while True:
    SellPrice = getGOLD()
    print("黄金赎回价:"+SellPrice)
    # 如果赎回价格超过567.97就给QQ邮箱发消息提醒
    if float(SellPrice) > 567.97:
        msg_qq(SellPrice)
    time.sleep(5) # 停5秒再请求

效果展示

  • 7
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小白要努力变黑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值