环境:windows7,python3.7
安装itchat,requests
金山词霸开放平台API:/dsapi
如果遇到登录微信时出现错误告警,请参照:微信登录告警解决方法
#!/usr/bin/env python# -*- coding: utf-8 -*-from threading import Timerfrom itchat.content import *import requestsimport itchat#获取金山词霸每日一句英语def getNews():#打开金山词霸开放平台APIurl = "/dsapi"req = requests.get(url)#"content":"Interests are anchors, and I believe they will bring peace and even happiness in the end."contents = req.json()['content']trans = req.json()['translation']return contents,trans#发送消息def sendNews():#global my_lovetry:#会弹出网页二维码扫描登录微信itchat.auto_login() #不想每次都扫描,登录时预配置#itchat.auto_login(hotReload=True)#itchat.run()#1.想给谁发信息,先找到该朋友,备注名my_friend = itchat.search_friends(name = r'小小‘’)#2.找到UserNamemy_love = my_friend[0]["UserName"]#new_cont, new_trans = getNews()#print(new_cont)#print(new_trans)msg1 = str(getNews()[0]) #获取金山词霸字典内容msg2 = str(getNews()[1][5:])msg = '翻译:'cont = str(msg+msg2)#3.发送消息#调试,直接给微信手机助手发送消息itchat.send(msg1,toUserName = "filehelper")itchat.send(cont,toUserName = 'filehelper')#给微信好友发送消息itchat.send(msg1,toUserName = my_love)itchat.send(cont,toUserName = my_love)#每隔86400秒发送一次,每天发送一次Timer(80000,sendNews).start()except:msg4 = "最爱你的人来啦!!!"itchat.send(msg4,toUserName = my_love)#itchat.logout()#调试函数def test():itchat.auto_login()itchat.send(u'测试消息发送','filehelper')#发送给文件助手if __name__ == '__main__':sendNews()