最近公司由于要时时监测网站的运行速度,特意给我们几个排了时间表,每天中午轮流监测,感觉人工监测做不到时时监测,而且还要点来点去有点麻烦,就自己用python写了一个脚本
本人水平有限,这段代码写的可能不优美,但可完成这次所需任务
上代码:
import time
import datetime
import itchat
import threading
import ctypes,sys
itchat.auto_login(hotReload=True,enableCmdQR=True)
# 你想发送人的备注名,但不能发送给自己,发送给自己只能用filehelper代替
users = itchat.search_friends(name=u'茹')
userName = users[0]['UserName']
# 清除购物车
def clear():
f = open("log/log"+str(datetime.date.today())+".txt",'a')
url='http://api1.dominos.com.cn/order.ashx?action=ClearShoppingCart&deviceid=C7855882B2C00001C2B6636013C54270&devicetype=wap&token=d67e911662c84041a52305fb2b0a1e54&callback=callback_json3'
req = request.Request(url)
res = request.urlopen(req)
if res.status==200:
print("Clear Success\n")
f.write("Clear Success\n")
else:
print("Clear Fail\n")
f.write("Clear Fail\n")
f.write('*' * 3+"end"+'*' * 3)
f.write("\n")
print('*' * 3+"end"+'*' * 3)
# 添加商品并把时间发送给好友
def fun_timer():
f = open("log/log"+str(datetime.date.today())+".txt",'a')
starttime = datetime.datetime.now()
header_dict = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko'}
url='http://api1.dominos.com.cn/order.ashx?action=addProduct&storecode=96574&productionNo=1727e56f63fc45f7bff4ff53657ed2c4&number=1&Properties=%7B%22ExtendProperty%22%3A%5B%7B%22PropertyKey%22%3A%22%E9%85%8D%E6%96%99%22%2C%22Values%22%3A%5B%7B%22PropertyValue%22%3A%222%22%2C%22Quantity%22%3A1%2C%22ExtraData%22%3A%22%E8%8A%9D%E5%A3%AB%7CCheese%22%7D%2C%7B%22PropertyValue%22%3A%22A%22%2C%22Quantity%22%3A1%2C%22ExtraData%22%3A%22%E5%9F%B9%E6%A0%B9%7CBacon%22%7D%2C%7B%22PropertyValue%22%3A%22Ct%22%2C%22Quantity%22%3A1%2C%22ExtraData%22%3A%22%E6%A8%B1%E6%A1%83%E7%95%AA%E8%8C%84%7CCherry%20Tomato%22%7D%2C%7B%22PropertyValue%22%3A%22Ad%22%2C%22Quantity%22%3A1%2C%22ExtraData%22%3A%22%E7%89%9B%E6%B2%B9%E6%9E%9C%7Cavocado%22%7D%2C%7B%22PropertyValue%22%3A%22Es%22%2C%22Quantity%22%3A1%2C%22ExtraData%22%3A%22%E8%8A%9D%E5%A3%AB%E9%A3%8E%E5%91%B3%E9%85%B1%7Ccheese%20sauce%22%7D%2C%7B%22PropertyValue%22%3A%22Gc%22%2C%22Quantity%22%3A1%2C%22ExtraData%22%3A%22%E8%8A%9D%E5%A3%AB%E9%A3%8E%E5%91%B3%E9%B8%A1%E5%9D%97%7Ccheesy%20chicken%22%7D%5D%7D%2C%7B%22PropertyKey%22%3A%22%E9%85%B1%E6%96%99%22%2C%22Values%22%3A%5B%7B%22PropertyValue%22%3A%22Qb%22%2C%22Quantity%22%3A1%2C%22ExtraData%22%3A%22%E6%B3%95%E5%BC%8F%E7%BE%8E%E5%A5%B6%E6%BB%8B%E9%85%B1%7CMayonnaise%22%7D%5D%7D%5D%2C%22ProductSizeCode%22%3A%22S%22%2C%22PastryCode%22%3A%22H%22%7D&deviceid=C7855882B2C00001C2B6636013C54270&devicetype=wap&token=d67e911662c84041a52305fb2b0a1e54&callback=callback_json35'
req = request.Request(url,headers=header_dict)
res = request.urlopen(req)
print("***start***")
f.write("***start***\n")
f.write(str(datetime.datetime.now())+"\n")
if res.status==200:
res = res.read()
# print(res.decode(encoding='utf-8'))
endtime = datetime.datetime.now()
SpendSeconds=(endtime-starttime).seconds
spendTime=(endtime-starttime)
spendTime=str(spendTime);
itchat.send("添加成功,耗时:"+spendTime,toUserName = 'filehelper')
if SpendSeconds<2:
print("Add Success:"+spendTime)
f.write("Add Success:"+spendTime)
else:
print("Add Success,but SpendLongTime:"+spendTime)
f.write("Add Success,but SpendLongTime:"+spendTime)
f.write("\n")
clear()
else:
print("Add fail")
global timer
timer = threading.Timer(10, fun_timer)
timer.start()
timer = threading.Timer(1, fun_timer)
timer.start()
最后给python个赞,用别的语言写需要好多代码,但python只用了短短几十行