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,translation16 def login_wechat():17 18 global bot19 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:])