1. 程式人生 > >python實現微信每日一句自動傳送給喜歡的人

python實現微信每日一句自動傳送給喜歡的人

# -*- coding: utf-8 -*-
'''
這是一個用來測試微信自動傳送訊息的demo
參考了一篇部落格
部落格地址:http://www.cnblogs.com/botoo/p/8622379.html
恩,主要就是用到了一個微信庫--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()