Python 測(cè)試api訪問(wèn)時(shí)間并發(fā)送給指定微信好友

最近公司由于要時(shí)時(shí)監(jiān)測(cè)網(wǎng)站的運(yùn)行速度,特意給我們幾個(gè)排了時(shí)間表,每天中午輪流監(jiān)測(cè),感覺(jué)人工監(jiān)測(cè)做不到時(shí)時(shí)監(jiān)測(cè),而且還要點(diǎn)來(lái)點(diǎn)去有點(diǎn)麻煩,就自己用python寫了一個(gè)腳本

本人水平有限,這段代碼寫的可能不優(yōu)美,但可完成這次所需任務(wù)

上代碼:

import time
import datetime
import itchat
import threading
import ctypes,sys
itchat.auto_login(hotReload=True,enableCmdQR=True)
# 你想發(fā)送人的備注名,但不能發(fā)送給自己,發(fā)送給自己只能用filehelper代替
users = itchat.search_friends(name=u'茹')
userName = users[0]['UserName']
# 清除購(gòu)物車
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)
# 添加商品并把時(shí)間發(fā)送給好友
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("添加成功,耗時(shí):"+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個(gè)贊,用別的語(yǔ)言寫需要好多代碼,但python只用了短短幾十行

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容