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

用python实现微信定时发文

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

标签:path   import   on()   friends   login   imp   http   cache   金山词霸   

 

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

 

用python实现微信定时发文

标签:path   import   on()   friends   login   imp   http   cache   金山词霸   

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

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