pip install wxpy
pip install schedule
Timer實現定時
wxpy是專門用於python處理個人用戶微信的相關模塊,這個模塊可以查看朋友、查看群組、發信息、公眾號操作等等,功能非常強大。
1 2 from __future__ import unicode_literals 3 from threading import Timer 4 from wxpy import * 5 import requests 6 bot = None 7 def get_news1(): 8 #獲取金山詞霸每日一句,英文和翻譯 9 url = "http://open.iciba.com/dsapi/" 10 r = requests.get(url) 11 print(r.json()) 12 contents = r.json()['content'] 13 note = r.json()['note'] 14 translation = r.json()['translation'] 15 return contents,note,translation 16 def login_wechat(): 17 18 global bot 19 bot = Bot() 20 # bot = Bot(console_qr=2,cache_path="botoo.pkl")#Linux專用,像素二維碼 21 22 def send_news(): 23 if bot == None: 24 login_wechat() 25 try: 26 my_friend = bot.friends().search(u'卿塵')[0] #你朋友的微信名稱,不是備注,也不是微信帳號。 27 #my_friend = bot.groups().search(u'燈火闌珊處')[0] #你群的微信名稱,不是備注,也不是微信帳號。 28 my_friend.send(get_news1()[0]) 29 my_friend.send(get_news1()[1]) 30 my_friend.send(get_news1()[2]) 31 #my_friend.send(get_news1()[1][5:]) 32 #t = Timer(86400, send_news) #每86400秒(1天),發送1次,不用linux的定時任務是因為每次登陸都需要掃描二維碼登陸,很麻煩的一件事,就讓他一直掛着吧 33 t = Timer(120, send_news) 34 t.start() 35 except: 36 print(u"今天消息發送失敗了") 37 if __name__ == "__main__": 38 send_news() 39 #print(get_news1()[0]) 40 #print(get_news1()[1][5:])