微信,爬取每日一句,文本,schedule函数定时发送消息

schedule函数定时发送消息,Bot(console_qr=2,cache_path="botoo.pkl")

 1 # -*- coding: utf-8 -*-
 2 #from __future__ import unicode_literals
 3 from threading import Timer
 4 from wxpy import *
 5 import requests
 6 import  schedule
 7 import  time
 8 
 9 bot = None
10 def get_news1():
11     #获取金山词霸每日一句,英文和翻译
12     url = "http://open.iciba.com/dsapi/"
13     r = requests.get(url)
14     print(r.json())
15     contents = r.json()['content']
16     note = r.json()['note']
17     translation = r.json()['translation']
18     return contents,note,translation
19 def login_wechat():
20     
21     global bot
22     #bot = Bot()
23     bot = Bot(console_qr=2,cache_path="botoo.pkl")#Linux专用,像素二维码
24  
25 def send_news():
26 
27     try:
28         my0_friend = bot.friends().search(u'王琳杰')[0]    #你朋友的微信名称,不是备注,也不是微信帐号。
29         my1_friend = bot.friends().search(u'浮生若梦')[0] 
30         #my2_friend = bot.friends().search(u'e.g.')[0] 
31 
32         my0_groups = bot.groups().search(u'聊天机器人测试')[0]    #你群的微信名称,不是备注,也不是微信帐号。
33         my1_groups = bot.groups().search(u'测试')[0] 
34 
35         my0_friend.send(get_news1()[0])
36         #my_friend.send(get_news1()[1])
37         #my_friend.send(get_news1()[2])
38         #my_friend.send(get_news1()[1][5:])
39         my1_friend.send(get_news1()[0])
40         #my2_friend.send(get_news1()[0])
41 
42         my0_groups.send(get_news1()[0])
43         my1_groups.send(get_news1()[0])
44         #t = Timer(86400, send_news) #每86400秒(1天),发送1次,不用linux的定时任务是因为每次登陆都需要扫描二维码登陆,很麻烦的一件事,就让他一直挂着吧
45         t = Timer(5, send_news) 
46         t.start()
47     except:
48         print(u"今天消息发送失败了")
49 
50 def run():
51 
52     schedule.every().day.at("21:57").do(send_news) #规定每天12:30执行job()函数
53     while True:
54         schedule.run_pending()#确保schedule一直运行
55         time.sleep(1)
56 
57 
58 if __name__ == "__main__":
59     if bot == None:
60         login_wechat()
61     run()
62     #print(get_news1()[0])
63     #print(get_news1()[1][5:])

发送文本消息

 1 # -*- coding: utf-8 -*-
 2 #from __future__ import unicode_literals
 3 from threading import Timer
 4 from wxpy import *
 5 import requests
 6 import  schedule
 7 import  time
 8 
 9 bot = None
10 def get_news1():
11     #获取金山词霸每日一句,英文和翻译
12     url = "http://open.iciba.com/dsapi/"
13     r = requests.get(url)
14     #print(r.json())
15     contents = r.json()['content']
16     note = r.json()['note']
17     translation = r.json()['translation']
18     return contents,note,translation
19 def login_wechat():
20     
21     global bot
22     #bot = Bot()
23     bot = Bot(console_qr=2,cache_path="botoo.pkl")#Linux专用,像素二维码
24  
25 def send_news():
26 
27     try:
28         my0_friend = bot.friends().search(u'王琳杰')[0]    #你朋友的微信名称,不是备注,也不是微信帐号。
29         my1_friend = bot.friends().search(u'浮生若梦')[0] 
30         #my2_friend = bot.friends().search(u'e.g.')[0] 
31 
32         my0_groups = bot.groups().search(u'聊天机器人测试')[0]    #你群的微信名称,不是备注,也不是微信帐号。
33         my1_groups = bot.groups().search(u'测试')[0] 
34 
35         my0_friend.send(get_news1()[0])
36         #my_friend.send(get_news1()[1])
37         #my_friend.send(get_news1()[2])
38         #my_friend.send(get_news1()[1][5:])
39         my1_friend.send(get_news1()[0])
40         #my2_friend.send(get_news1()[0])
41 
42         my0_groups.send(get_news1()[0])
43         my1_groups.send(get_news1()[0])
44 
45 
46         my0_friend.send(u'打扰一下,正在测试……')
47         my1_friend.send(u'打扰一下,正在测试……')
48         my0_groups.send(u'打扰一下,正在测试……')
49         my1_groups.send(u'打扰一下,正在测试……')
50 
51     except:
52         print(u"今天消息发送失败了")
53 
54 def run():
55 
56     schedule.every().day.at("12:30").do(send_news) #规定每天12:30执行job()函数
57     while True:
58         schedule.run_pending()#确保schedule一直运行
59         time.sleep(1)
60 
61 
62 if __name__ == "__main__":
63     if bot == None:
64         login_wechat()
65     run()
66     #print(get_news1()[0])
67     #print(get_news1()[1][5:])
原文地址:https://www.cnblogs.com/wanglinjie/p/9291706.html