本文實例為大家分享了python實現微信每日一句自動發送的具體代碼,供大家參考,具體內容如下
參考了一篇博客:教你使用python實現微信每天給女朋友說晚安
代碼:
# -*- coding: utf-8 -*- ''' 這是一個用來測試微信自動發送消息的demo 恩,主要就是用到了一個微信庫--wxpy 安裝很簡單 pip install wxpy 下面就開始吧 主要就兩個函數 1、getNews();用以獲取信息 2、sendNews();用以發送信息 我這里發送消息用的是for循環本意是群發,但是!但是!但是!程序發的太快會被微信禁掉,大概40個人左右就會被禁,以后可以試試sleep一下。 另外vscode中自定義python編譯器: Ctrl+shift+p, 選擇 python: Select Interpreter ''' from __future__ import unicode_literals from wxpy import * import requests from threading import Timer itchat = Bot(console_qr=2,cache_path="botoo.pkl") def getNews(): url = "http://open.iciba.com/dsapi/" r = requests.get(url) content = r.json()['content'] note = r.json()['note'] return content, note def sendNews(): try: #這里是備注 friend = itchat.friends().search(name = u'xxx') content = getNews() print(content) message1 = str(content[0]) message2 = str(content[1]) message3 = "xxx" print(friend) for index,item in enumerate(friend): print("發送給 "+str(item)+" ing,index="+str(index)) item.send(message1) item.send(message2) item.send(message3) t = Timer(86400,sendNews) t.start() except: errorMessage = "xxx" for index,item in enumerate(friend): item.send(errorMessage) if __name__ == "__main__": sendNews()
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。