微信暫時發現有2個好用的模塊,wxpy和itchat,直接上代碼
#encoding:utf-8 #QQ496631085 小和 此代碼是固定給某個好友發消息 from threading import Timer from wxpy import * import requests bot = Bot()#連接微信,會出現一個登陸微信的二維碼 def get_news(): '''獲取金山詞霸每日一句''' url = 'http://open.iciba.com/dsapi' r = requests.get(url) content = r.json()['content'] note = r.json()['note'] return content,note def send_news(): try: contents = get_news() my_friend =bot.friends().search(u'小和')[0]#這里是搜索你微信好友里面的的昵稱 my_friend.send(contents[0])#給好友發送消息 my_friend.send(contents[1]) my_friend.send(u'^_^') # t = Timer(86400,send_news)#這里是一天發送一次,86400s = 24h # t.start() # friends=bot.friends() # print(friends) # for x in friends: # print(x) except: my_friend = bot.friends().search('XiaoHe')[0]#這里是你的微信昵稱 my_friend.send(u'今天消息發送失敗了') send_news()
給所有好友發消息代碼如下
