码迷,mamicode.com
首页 > 微信 > 详细

微信,爬取每日一句,发送至多人,多个群

时间:2018-07-12 21:47:07      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:int   需要   url   on()   nbsp   note   start   except   bsp   

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:])

 

微信,爬取每日一句,发送至多人,多个群

标签:int   需要   url   on()   nbsp   note   start   except   bsp   

原文地址:https://www.cnblogs.com/wanglinjie/p/9291658.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!