Python下发送定时消息给微信好友

本文介绍了一种使用Python的wxpy库实现微信自动化的方法,具体为定时向指定好友发送金山词霸每日一句及其翻译。通过结合requests库获取网络数据,threading模块的Timer函数实现定时任务,展示了如何在微信中自动化消息发送。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 1 """
 2 Description:时间可以改长一点  一分钟一个
 3 Author:Nod
 4 Date:
 5 Record:
 6 #---------------------------------v1-----------------------------------#
 7 """
 8 
 9 
10 
11 from __future__ import unicode_literals
12 from threading import Timer
13 from wxpy import *
14 import requests
15 
16 bot = Bot()
17 
18 # linux执行登陆请调用下面的这句
19  #bot = Bot(console_qr=2,cache_path="botoo.pkl")
20 #获取金山词霸每日一句,英文和翻译
21 def get_news():
22     url = "http://open.iciba.com/dsapi/"
23     r = requests.get(url)
24     content = r.json()['content']
25     note = r.json()['note']
26     return content, note
27 
28 def send_news():
29    try:
30      contents = get_news()
31 
32    # 你朋友的微信备注,请注意最好你的好友备注只有1个
33 
34      my_friend = bot.friends().search(u'季学远')[0]
35      my_friend.send(contents[0])
36      my_friend.send(contents[1])
37      my_friend.send(u"Have a good one!")
38      # 每86400秒(1天),发送1次
39      t = Timer(86400, send_news)
40      t.start()
41    except:
42 
43 
44 
45        # 你的微信名称,不是微信帐号。
46 
47         my_friend = bot.friends().search('季学远')[0]
48         my_friend.send(u"今天消息发送失败了")
49 
50 if __name__ == "__main__":
51     send_news()

 

 

pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple requests
pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple wxpy

转载于:https://www.cnblogs.com/nodchen/p/9602035.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值