python监控vps是否有货(钉钉报警提醒)
最近一直在找稳定的vps(你们懂的),这样才能看遍全世界,最近看上了几个vps,但想买的时候没货,我又不想一直自己去刷网页关注,所以就写了个脚本来帮我监控着,为了方便接收到消息,我加入了钉钉来进行提醒.
系统:centos 7.x(64位)
cat /root/soft_shell/vps.py
#!/usr/bin/env python3 # -*- coding: UTF-8 -*- from urllib import request import requests import json from datetime import datetime from dingtalkchatbot.chatbot import DingtalkChatbot time = datetime.now().strftime('%Y-%m-%d %H:%M:%S') url_id = [74,75] webhook = "https://oapi.dingtalk.com/robot/send?access_token=这里写你自己的key" title = "上货监测" header = { "Content-Type": "application/json", "Charset": "UTF-8" } print('开始检查是否有货:') try: flag=0 while len(url_id) > 0 : pid=url_id.pop() url='https://manage.hostdare.com/cart.php?a=add&pid=%s' % (pid) head={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36'} req=request.Request(url,headers=head) page=request.urlopen(req).read() if str(page).find('out of stock')>0: flag=0 print('无货') else: flag=flag+1 print('有货') tex = "上货啦" + "\n" + url if flag<3: message={ "msgtype": "text", "text": { "content": tex }, "at": { "isAtAll": False } } message_json = json.dumps(message) requests.post(url=webhook,data=message_json,headers=header) except: print('脚本异常,退出')
至于监控的频繁,大家可以根据自己的喜好去搞.
评论: