python实现微信定时给好友说晚安
Python趣味编程:给她说晚安
接下来代码跟上:
from __future__ import unicode_literals
from threading import Timer
from wxpy import *
import requests
bot = Bot(cache_path=True)
def get_news():
url = "http://open.iciba.com/dsapi/"
r = requests.get(url)
contents = r.json()['content']
translation = r.json()['translation']
return contents,translation
def send_news():
try:
my_friend = bot.friends().search(u'好友的名称')
my_friend.send(u"XXX") #XXX:你想说的话
my_friend.send(get_news()[0])
my_friend.send(get_news()[1][8:])
t = Timer(XXX,send_news) #XXX:时间(整数),默认单位秒
t.start()
except:
my_friend = bot.friends().search('你的微信名称')[0]
my_friend.send(u'发送消息失败!')
if __name__=="__main__":
send_news()
注:不懂的代码可以上网查一下哦,都可以查到;
大家也可以对此修改,实现对多个好友聊天。