黄金白银价格监控+钉钉和企微机器人
本帖最后由 天域至尊 于 2023-4-13 11:05 编辑该脚本主要用于对贵金属价格的监控,主要是国内基础金价、基础银价、au(t+d)、ag(t+d)等
请注意,监控的数据来源是上海贵金属交易所的价格。
所谓基础金价,基础银价。就是国内标定的当日最低价格,金店、银行等金价会根据此价格向上浮动。
假如你买一根金条或者金首饰,他的金额组成是由下面内容组成的:
基础金价+工费+火耗+商家利润
而此脚本,只监控基础金价变动,其也反映了市场金价的情况。
有没有可能以基础金价买金子呢?可以,去上海市黄金交易所,最低只能买1KG黄金,而且还不管退换。
运行结果在钉钉提醒,所以你要增加钉钉机器人,效果如下:
企业微信效果如下:
代码如下:
import requests
from lxml import etree
#这里是黄金的提醒价,低于此价格会@全体成员
au_remind_price=390.0
#这里是白银的提醒价,低于此价格会@全体成员
ag_remind_price=4900.0
robot_url=["这里存放你的钉钉/企业微信机器人链接,可使用列表存储多个,会同时发送"]
url="https://www.sge.com.cn/sjzx/yshqbg"
def requests_hold(url):
header={
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Firefox/102.0",
"Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
"Accept-Language":"zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2",
"Connection":"keep-alive"
}
for i in range(0,10):
try:
r=requests.get(url=url,timeout=60,headers=header)
return r.text
except Exception:
pass
return False
def xpath_data(url):
"""
该函数用于从网页解析和整理黄金白银价格
"""
time_xpath="/html/body/div/div/div/div/div/div/p/span/text()"
au9999_new_xpath="/html/body/div/div/div/div/div/div/div/table/tr/td/span/text()"
au9999_high_xpath="/html/body/div/div/div/div/div/div/div/table/tr/td/text()"
au9999_low_xpath="/html/body/div/div/div/div/div/div/div/table/tr/td/text()"
au9999_open_today_xpath="/html/body/div/div/div/div/div/div/div/table/tr/td/text()"
Au100g_new_xpath="/html/body/div/div/div/div/div/div/div/table/tr/td/span/text()"
Au100g_high_xpath="/html/body/div/div/div/div/div/div/div/table/tr/td/text()"
Au100g_low_xpath="/html/body/div/div/div/div/div/div/div/table/tr/td/text()"
Au100g_open_today_xpath="/html/body/div/div/div/div/div/div/div/table/tr/td/text()"
auTD_new_xpath="/html/body/div/div/div/div/div/div/div/table/tr/td/span/text()"
auTD_high_xpath="/html/body/div/div/div/div/div/div/div/table/tr/td/text()"
auTD_low_xpath="/html/body/div/div/div/div/div/div/div/table/tr/td/text()"
auTD_open_today_xpath="/html/body/div/div/div/div/div/div/div/table/tr/td/text()"
agTD_new_xpath="/html/body/div/div/div/div/div/div/div/table/tr/td/span/text()"
agTD_high_xpath="/html/body/div/div/div/div/div/div/div/table/tr/td/text()"
agTD_low_xpath="/html/body/div/div/div/div/div/div/div/table/tr/td/text()"
agTD_open_today_xpath="/html/body/div/div/div/div/div/div/div/table/tr/td/text()"
data=requests_hold(url)
html = etree.HTML(data)
time_data=html.xpath(time_xpath)
au9999_new=html.xpath(au9999_new_xpath)
au9999_high=html.xpath(au9999_high_xpath)
au9999_low=html.xpath(au9999_low_xpath)
au9999_open_today=html.xpath(au9999_open_today_xpath)
Au100g_new=html.xpath(Au100g_new_xpath)
Au100g_high=html.xpath(Au100g_high_xpath)
Au100g_low=html.xpath(Au100g_low_xpath)
Au100g_open_today=html.xpath(Au100g_open_today_xpath)
auTD_new=html.xpath(auTD_new_xpath)
auTD_high=html.xpath(auTD_high_xpath)
auTD_low=html.xpath(auTD_low_xpath)
auTD_open_today=html.xpath(auTD_open_today_xpath)
agTD_new=html.xpath(agTD_new_xpath)
agTD_high=html.xpath(agTD_high_xpath)
agTD_low=html.xpath(agTD_low_xpath)
agTD_open_today=html.xpath(agTD_open_today_xpath)
return {
"time":time_data,
"au9999_new":au9999_new,
"au9999_high":au9999_high,
"au9999_low":au9999_low,
"au9999_open_today":au9999_open_today,
"Au100g_new":Au100g_new,
"Au100g_high":Au100g_high,
"Au100g_low":Au100g_low,
"Au100g_open_today":Au100g_open_today,
"auTD_new":auTD_new,
"auTD_high":auTD_high,
"auTD_low":auTD_low,
"auTD_open_today":auTD_open_today,
"agTD_new":agTD_new,
"agTD_high":agTD_high,
"agTD_low":agTD_low,
"agTD_open_today":agTD_open_today
}
def check_price_data(price_data:dict):
"""
该函数用于判断要不要发起提醒和艾特全体成员
"""
global au_remind_price,ag_remind_price
au9999_new=float(price_data["au9999_new"])
au9999_high=float(price_data["au9999_high"])
au9999_low=float(price_data["au9999_low"])
au9999_open_today=float(price_data["au9999_open_today"])
Au100g_new=float(price_data["Au100g_new"])
Au100g_high=float(price_data["Au100g_high"])
Au100g_low=float(price_data["Au100g_low"])
Au100g_open_today=float(price_data["Au100g_open_today"])
auTD_new=float(price_data["auTD_new"])
auTD_high=float(price_data["auTD_high"])
auTD_low=float(price_data["auTD_low"])
auTD_open_today=float(price_data["auTD_open_today"])
agTD_new=float(price_data["agTD_new"])
agTD_high=float(price_data["agTD_high"])
agTD_low=float(price_data["agTD_low"])
agTD_open_today=float(price_data["agTD_open_today"])
remain={
"at":False,
"word":""
}
if au9999_new<=au_remind_price:
remain["at"]=True
remain["word"]=remain["word"]+">au9999最新价低于提醒价%.2f "%(au_remind_price)+"\n\n"
if au9999_high<=au_remind_price:
remain["at"]=True
remain["word"]=remain["word"]+">au9999最高价低于提醒价%.2f "%(au_remind_price)+"\n\n"
if au9999_low<=au_remind_price:
remain["at"]=True
remain["word"]=remain["word"]+">au9999最低价低于提醒价%.2f "%(au_remind_price)+"\n\n"
if au9999_open_today<=au_remind_price:
remain["at"]=True
remain["word"]=remain["word"]+">au9999今开价低于提醒价%.2f "%(au_remind_price)+"\n\n"
if Au100g_new<=au_remind_price:
remain["at"]=True
remain["word"]=remain["word"]+">Au100g最新价低于提醒价%.2f "%(au_remind_price)+"\n\n"
if Au100g_high<=au_remind_price:
remain["at"]=True
remain["word"]=remain["word"]+">Au100g最高价低于提醒价%.2f "%(au_remind_price)+"\n\n"
if Au100g_low<=au_remind_price:
remain["at"]=True
remain["word"]=remain["word"]+">Au100g最低价低于提醒价%.2f "%(au_remind_price)+"\n\n"
if Au100g_open_today<=au_remind_price:
remain["at"]=True
remain["word"]=remain["word"]+">Au100g今开价低于提醒价%.2f "%(au_remind_price)+"\n\n"
if auTD_new<=au_remind_price:
remain["at"]=True
remain["word"]=remain["word"]+">auTD最新价低于提醒价%.2f "%(au_remind_price)+"\n\n"
if auTD_high<=au_remind_price:
remain["at"]=True
remain["word"]=remain["word"]+">auTD最高价低于提醒价%.2f "%(au_remind_price)+"\n\n"
if auTD_low<=au_remind_price:
remain["at"]=True
remain["word"]=remain["word"]+">auTD最低价低于提醒价%.2f "%(au_remind_price)+"\n\n"
if auTD_open_today<=au_remind_price:
remain["at"]=True
remain["word"]=remain["word"]+">auTD今开价低于提醒价%.2f "%(au_remind_price)+"\n\n"
if agTD_new<=ag_remind_price:
remain["at"]=True
remain["word"]=remain["word"]+">agTD最新价低于提醒价%.2f "%(ag_remind_price)+"\n\n"
if agTD_high<=ag_remind_price:
remain["at"]=True
remain["word"]=remain["word"]+">agTD最高价低于提醒价%.2f "%(ag_remind_price)+"\n\n"
if agTD_low<=ag_remind_price:
remain["at"]=True
remain["word"]=remain["word"]+">agTD最低价低于提醒价%.2f "%(ag_remind_price)+"\n\n"
if agTD_open_today<=ag_remind_price:
remain["at"]=True
remain["word"]=remain["word"]+">agTD今开价低于提醒价%.2f "%(ag_remind_price)+"\n\n"
return remain
def send_word(robot_url:list,price_data:dict,remian:dict):
"""
该函数向钉钉机器人发送提醒消息
"""
au9999_word=">**Au9999**\n>>最新价**%s元** 最高价**%s元**\n\n>>最低价**%s元** 今开盘**%s元**\n\n"%(price_data["au9999_new"],price_data["au9999_high"],price_data["au9999_low"],price_data["au9999_open_today"])
Au100g_word=">**Au100g**\n>>最新价**%s元** 最高价**%s元**\n\n>>最低价**%s元** 今开盘**%s元**\n\n"%(price_data["Au100g_new"],price_data["Au100g_high"],price_data["Au100g_low"],price_data["Au100g_open_today"])
auTD_word=">**Au(T+D)**\n>>最新价**%s元** 最高价**%s元**\n\n>>最低价**%s元** 今开盘**%s元**\n\n"%(price_data["auTD_new"],price_data["auTD_high"],price_data["auTD_low"],price_data["auTD_open_today"])
agTD_word=">**Ag(T+D)**\n>>最新价**%s元** 最高价**%s元**\n\n>>最低价**%s元** 今开盘**%s元**\n\n"%(price_data["agTD_new"],price_data["agTD_high"],price_data["agTD_low"],price_data["agTD_open_today"])
remain_word=""
if remian["at"]:
remain_word="### 提醒信息\n "+remian["word"]
header={
"Content-Type":"application/json"
}
text="# %s\n "%(price_data["time"])+"## 黄金价格\n "+au9999_word+Au100g_word+auTD_word+"## 白银价格\n "+agTD_word+remain_word
if price_data["au9999_new"]=="0.0":
text="# %s\n "%(price_data["time"])+"## 今日无数据\n "
remian["at"]=False
dingtalk_body_data=[{
"msgtype": "markdown",
"markdown": {
"title":"贵金属提醒",
"text": text
},
"at": {
"atMobiles": [
],
"atUserIds": [
],
"isAtAll": remian["at"]
}
}]
weixin_qq_body=[{
"msgtype": "markdown",
"markdown": {
"content": text
}
}]
if remian["at"]:
weixin_qq_body.append({
"msgtype": "text",
"text": {
"content": "",
"mentioned_list":["@all"],
"mentioned_mobile_list":[]
}
})
for robot_url_now in robot_url:
if "dingtalk.com" in robot_url_now:
body_data=dingtalk_body_data
elif "weixin.qq.com" in robot_url_now:
body_data=weixin_qq_body
else:
print("%s 该url未能识别为钉钉机器人,或者为企微机器人。")
continue
for body_now in body_data:
for i in range(0,3):
try:
r=requests.post(url=robot_url_now,headers=header,timeout=60,json=body_now)
break
except Exception as err:
print(err)
def main(url,robot_url):
"""
主函数,调用其他函数
"""
price_data=xpath_data(url=url)
remain_data=check_price_data(price_data=price_data)
send_word(robot_url=robot_url,remian=remain_data,price_data=price_data)
main(url=url,robot_url=robot_url)
在第8行列表中,写入自己钉钉或者企业微信的机器人URL,程序会自己识别,进行发送。
周期提醒建议在Linux部署,使用计划任务。比如,下面就是每天早上十点发送提醒消息。
> crontab -e
0 10 * * * python3 /root/gold_monitor_robot.py
建议每天早上十点钟设置定时任务,因为过早站点数据可能未更新,导致采集数据为0.0。
更新日志:
1.增加了数据为0.0时的异常处理。
2022年12月28日更新日志:
1.增加反爬措施
2023年04月13日更新日志:
1.修改了请求头,防止被封禁
天域至尊 发表于 2022-12-10 01:43
大佬有啥需要吗
我刷了个玩心云的机器 到用linux创建文件夹后,读取不了文件夹 ,输入 下载连接 下载不了文件,很奇怪,大佬看看 是什么问题呢?
login as: root
root@192.168.1.16's password:
____ ____ ____
/ ___| ( _ )/ |___ \
\___ \ / _ \| | __) |
___) | (_) | |/ __/
|____/ \___/|_|_____|
Welcome to Armbian 20.12 Buster with Linux 5.9.0-rc7-aml-s812
No end-user support: built from trunk
System load: 2% Up time: 1:05
Memory usage:7% of 989M IP: 192.168.1.16
CPU temp: 23°C Usage of /: 17% of 6.5G
[ 0 security updates available, 22 updates total: apt upgrade ]
Last check: 2020-12-13 22:52
[ General system configuration (beta): armbian-config ]
Last login: Sun Dec 13 23:45:29 2020 from 192.168.1.12
root@aml-s812:~# Phantom可 发表于 2022-12-12 10:19
我刷了个玩心云的机器 到用linux创建文件夹后,读取不了文件夹 ,输入 下载连接 下载不了文件,很奇怪, ...
有报错内容吗 学习了 ,膜拜大佬,~~~~ 谢谢我先囤528斤金子 学习学习,谢谢分享 行情数据是需要实时了解的,感谢分享。 写了一个小爬虫? 能给制作一个小软件吗?谢谢 chayunyuxiang 发表于 2022-12-10 12:54
能给制作一个小软件吗?谢谢
啥需求呢? 这个怎么使用呢 大佬增加企业微信提醒吧