1. 程式人生 > >微信,爬取每日一句,發送至多人,多個群

微信,爬取每日一句,發送至多人,多個群

ever ear con nbsp ret gin 定時任務 linu url

Timer(5, send_news)

每日一句,發送至多人,多個群

 1 # -*- coding: utf-8 -*-
 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 my0_friend = bot.friends().search(u王琳傑)[0] #你朋友的微信名稱,不是備註,也不是微信帳號。 27 my1_friend = bot.friends().search(u浮生若夢)[0] 28 #my2_friend = bot.friends().search(u‘e.g.‘)[0] 29 30 my0_groups = bot.groups().search(u
聊天機器人測試)[0] #你群的微信名稱。 31 my1_groups = bot.groups().search(u測試)[0] 32 33 my0_friend.send(get_news1()[0]) 34 #my_friend.send(get_news1()[1]) 35 #my_friend.send(get_news1()[2]) 36 #my_friend.send(get_news1()[1][5:]) 37 my1_friend.send(get_news1()[0]) 38 #my2_friend.send(get_news1()[0]) 39 40 my0_groups.send(get_news1()[0]) 41 my1_groups.send(get_news1()[0]) 42 #t = Timer(86400, send_news) #每86400秒(1天),發送1次,不用linux的定時任務是因為每次登陸都需要掃描二維碼登陸,很麻煩的一件事,就讓他一直掛著吧 43 t = Timer(5, send_news) 44 t.start() 45 except: 46 print(u"今天消息發送失敗了") 47 48 ‘‘‘ 49 schedule.every().day.at("12:30").do(send_news) #規定每天12:30執行job()函數 50 while True: 51 schedule.run_pending()#確保schedule一直運行 52 time.sleep(1) 53 ‘‘‘ 54 55 if __name__ == "__main__": 56 send_news() 57 #print(get_news1()[0]) 58 #print(get_news1()[1][5:])

微信,爬取每日一句,發送至多人,多個群