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

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

 

转载于:https://www.cnblogs.com/wanglinjie/p/9291658.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值