吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 3094|回复: 39
收起左侧

[Python 原创] 黄金白银价格监控+钉钉和企微机器人

[复制链接]
天域至尊 发表于 2022-12-10 11:09
本帖最后由 天域至尊 于 2023-4-13 11:05 编辑

该脚本主要用于对贵金属价格的监控,主要是国内基础金价、基础银价、au(t+d)、ag(t+d)等

请注意,监控的数据来源是上海贵金属交易所的价格。

所谓基础金价,基础银价。就是国内标定的当日最低价格,金店、银行等金价会根据此价格向上浮动。

假如你买一根金条或者金首饰,他的金额组成是由下面内容组成的:
    基础金价+工费+火耗+商家利润

而此脚本,只监控基础金价变动,其也反映了市场金价的情况。

有没有可能以基础金价买金子呢?可以,去上海市黄金交易所,最低只能买1KG黄金,而且还不管退换。

运行结果在钉钉提醒,所以你要增加钉钉机器人,效果如下:
QQ截图20221210110850.png

企业微信效果如下:
QQ截图20221210143540.png
代码如下:
[Python] 纯文本查看 复制代码
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[6]/div/div[2]/div[2]/div[2]/div[1]/p/span[2]/text()"
    au9999_new_xpath="/html/body/div[6]/div/div[2]/div[2]/div[2]/div[2]/div/table/tr[2]/td[2]/span/text()"
    au9999_high_xpath="/html/body/div[6]/div/div[2]/div[2]/div[2]/div[2]/div/table/tr[2]/td[3]/text()"
    au9999_low_xpath="/html/body/div[6]/div/div[2]/div[2]/div[2]/div[2]/div/table/tr[2]/td[4]/text()"
    au9999_open_today_xpath="/html/body/div[6]/div/div[2]/div[2]/div[2]/div[2]/div/table/tr[2]/td[5]/text()"

    Au100g_new_xpath="/html/body/div[6]/div/div[2]/div[2]/div[2]/div[2]/div/table/tr[4]/td[2]/span/text()"
    Au100g_high_xpath="/html/body/div[6]/div/div[2]/div[2]/div[2]/div[2]/div/table/tr[4]/td[3]/text()"
    Au100g_low_xpath="/html/body/div[6]/div/div[2]/div[2]/div[2]/div[2]/div/table/tr[4]/td[4]/text()"
    Au100g_open_today_xpath="/html/body/div[6]/div/div[2]/div[2]/div[2]/div[2]/div/table/tr[4]/td[5]/text()"

    auTD_new_xpath="/html/body/div[6]/div/div[2]/div[2]/div[2]/div[2]/div/table/tr[7]/td[2]/span/text()"
    auTD_high_xpath="/html/body/div[6]/div/div[2]/div[2]/div[2]/div[2]/div/table/tr[7]/td[3]/text()"
    auTD_low_xpath="/html/body/div[6]/div/div[2]/div[2]/div[2]/div[2]/div/table/tr[7]/td[4]/text()"
    auTD_open_today_xpath="/html/body/div[6]/div/div[2]/div[2]/div[2]/div[2]/div/table/tr[7]/td[5]/text()"

    agTD_new_xpath="/html/body/div[6]/div/div[2]/div[2]/div[2]/div[2]/div/table/tr[6]/td[2]/span/text()"
    agTD_high_xpath="/html/body/div[6]/div/div[2]/div[2]/div[2]/div[2]/div/table/tr[6]/td[3]/text()"
    agTD_low_xpath="/html/body/div[6]/div/div[2]/div[2]/div[2]/div[2]/div/table/tr[6]/td[4]/text()"
    agTD_open_today_xpath="/html/body/div[6]/div/div[2]/div[2]/div[2]/div[2]/div/table/tr[6]/td[5]/text()"

    data=requests_hold(url)
    html = etree.HTML(data)
    time_data=html.xpath(time_xpath)[0]

    au9999_new=html.xpath(au9999_new_xpath)[0]
    au9999_high=html.xpath(au9999_high_xpath)[0]
    au9999_low=html.xpath(au9999_low_xpath)[0]
    au9999_open_today=html.xpath(au9999_open_today_xpath)[0]

    Au100g_new=html.xpath(Au100g_new_xpath)[0]
    Au100g_high=html.xpath(Au100g_high_xpath)[0]
    Au100g_low=html.xpath(Au100g_low_xpath)[0]
    Au100g_open_today=html.xpath(Au100g_open_today_xpath)[0]

    auTD_new=html.xpath(auTD_new_xpath)[0]
    auTD_high=html.xpath(auTD_high_xpath)[0]
    auTD_low=html.xpath(auTD_low_xpath)[0]
    auTD_open_today=html.xpath(auTD_open_today_xpath)[0]

    agTD_new=html.xpath(agTD_new_xpath)[0]
    agTD_high=html.xpath(agTD_high_xpath)[0]
    agTD_low=html.xpath(agTD_low_xpath)[0]
    agTD_open_today=html.xpath(agTD_open_today_xpath)[0]

    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部署,使用计划任务。比如,下面就是每天早上十点发送提醒消息。
[Shell] 纯文本查看 复制代码
> crontab -e
0 10 * * * python3 /root/gold_monitor_robot.py


建议每天早上十点钟设置定时任务,因为过早站点数据可能未更新,导致采集数据为0.0。
更新日志:
1.增加了数据为0.0时的异常处理。

2022年12月28日更新日志:
1.增加反爬措施

2023年04月13日更新日志:
1.修改了请求头,防止被封禁

免费评分

参与人数 7吾爱币 +13 热心值 +7 收起 理由
5455123 + 1 + 1 谢谢@Thanks!
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
ha0yu + 1 + 1 谢谢@Thanks!
WSYJJ + 1 + 1 谢谢@Thanks!
Tomato00152pj + 1 + 1 我很赞同!
painstaking1 + 1 + 1 谢谢@Thanks!
bot123 + 1 + 1 热心回复!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

Phantom可 发表于 2022-12-12 10:19

我刷了个玩心云的机器 到用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:~#
 楼主| 天域至尊 发表于 2022-12-12 17:14
Phantom可 发表于 2022-12-12 10:19
我刷了个玩心云的机器 到用linux创建文件夹后,读取不了文件夹 ,输入 下载连接 下载不了文件,很奇怪, ...

有报错内容吗
qq11xx 发表于 2022-12-10 11:45
lcg888 发表于 2022-12-10 11:59
谢谢我先囤528斤金子
zr2019 发表于 2022-12-10 12:10
学习学习,谢谢分享
雾都孤尔 发表于 2022-12-10 12:19
行情数据是需要实时了解的,感谢分享。
Penguinbupt 发表于 2022-12-10 12:46
写了一个小爬虫?
chayunyuxiang 发表于 2022-12-10 12:54
能给制作一个小软件吗?谢谢
 楼主| 天域至尊 发表于 2022-12-10 13:25
chayunyuxiang 发表于 2022-12-10 12:54
能给制作一个小软件吗?谢谢

啥需求呢?
wpdzdx 发表于 2022-12-10 13:42
这个怎么使用呢
haile319 发表于 2022-12-10 13:47
大佬增加企业微信提醒吧
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-25 04:47

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表