本文共 1342 字,大约阅读时间需要 4 分钟。
金山词霸每日提供一句英文单词和翻译,作为学习者或爱好者,这是一个非常好的资源。通过编写简单的脚本,我们可以实现每天定时自动发送这句词霸到微信中,方便随时查看。
首先,我们需要安装两个库:
在命令提示符中输入以下命令安装:
pip install wxpy requests
以下是完整的Python脚本:
from wxpy import Botfrom requests import requestsfrom threading import Timerimport randombot = Bot()def get_words(): """获取金山词霸每日一句""" url = "http://open.iciba.com/dsapi/" response = requests.get(url) data = response.json() return data['content'], data['note']def send_to_friend(): try: content, note = get_words() # 替换为目标微信名称 friend = bot.friends().search('目标微信名')[0] friend.send(content) friend.send(note) friend.send(u"晚安") except Exception as e: print(f"发送失败:{str(e)}") # 替换为目标微信名称 fail_friend = bot.friends().search('目标微信名')[0] fail_friend.send(u"今天消息发送失败了")# 设置定时任务random_delay = random.randint(0, 100)timer = Timer(86400 + random_delay, send_to_friend)timer.start() 目标微信名为实际的目标微信名称bot.py并在IDE中运行86400(1天)调整为更短的时间requests库获取其他平台的每日一句数据通过上述方法,我们可以轻松实现每天定时发送金山词霸到微信的功能,方便随时查看和复习。
转载地址:http://tslfk.baihongyu.com/