目录
实现功能
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秒再请求