python itchat给指定联系人发消息的方法
作者:白学家Lynn 时间:2023-02-10 18:21:36
itchat模块
官方参考文档:https://itchat.readthedocs.io/zh/latest/
安装
pip install itchat / pip3 install itchat
原理
Python模仿网页版微信登陆,并且现有一套操作网页版微信的API,可以将你使用微信中产生的数据爬下来,并做出相应的处理。
操作
1.导入这套微信API的包itchat
import itchat
2.模仿网页版微信登陆
itchat.auto_login()
3.使用相关函数找到相关微信联系人信息(这里返回的是一个JOSN数组)
users=itchat.search_friends("飞叔Brother")
4.得到相关联系人的用户名(具体想看JOSN内部都是什么数据可以自己打印出来看看)
userName= users[0]['UserName']
5.发送信息到相关联系人
itchat.send('你好飞叔Brother',toUserName=userName)
至此,就会发送成功了。
import itchat
itchat.auto_login()
itchat.send('Hello, filehelper', toUserName='filehelper')
这段代码意思是给filehelper发送一个hello,filehelper就是文件助手。
那么我们想给指定的人发消息,并不是把filehelper改掉这么简单
users=itchat.search_friends("老王")
userName= users[0]['UserName']
print(userName)
itchat.send('你好老王',toUserName=userName)
如果我们想给老王发消息,就先使用itchat.search方法,会把所有备注名为老王的联系人全都找出来。
之后我们选取第一个(如果你的联系人列表里只有一个老王,那么就只会搜出来一个)
users[0]取到的是一个联系人对象,他里面有个key叫UserName,它就是真正的用户的username
之后我们再使用itchat.send方法,就可以成功向老王发送消息了
来源:https://blog.csdn.net/Lynn_coder/article/details/79436539
标签:python,itchat,指定联系人,发消息
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
看ASP程序源码的方法及工具
2009-01-21 19:58:00
python 对excel交互工具的使用详情
2021-11-25 19:10:06
![](https://img.aspxhome.com/file/2023/5/133565_0s.jpg)
python使用paramiko模块实现ssh远程登陆上传文件并执行
2021-01-09 00:35:03
Python绘图模块 turtle案例代码
2022-12-16 01:28:10
![](https://img.aspxhome.com/file/2023/2/134702_0s.png)
迎来2009年CSS裸奔节(CSS Naked Day )
2009-04-24 12:41:00
![](https://img.aspxhome.com/file/UploadPic/20094/24/qqqqqqqqq-36s.gif)
python模拟登录并且保持cookie的方法详解
2023-09-06 06:07:16
python如何将一个四位数反向输出
2023-03-21 16:42:40
python修改操作系统时间的方法
2022-10-14 14:13:29
高性能表现的网站(译)
2008-08-31 20:26:00
![](https://img.aspxhome.com/file/UploadPic/20088/31/2008831203229237s.gif)
用户体验中的五大要素
2008-07-07 16:41:00
安装2019Pycharm最新版本的教程详解
2023-06-25 23:27:54
![](https://img.aspxhome.com/file/2023/1/87481_0s.jpg)
Python fire模块(最简化命令行生成工具)的使用教程详解
2022-06-10 15:25:00
![](https://img.aspxhome.com/file/2023/0/105280_0s.png)
javascript面向对象编程(二)
2008-03-07 12:59:00
JavaScript实现字符串与日期的互相转换及日期的格式化
2024-06-05 09:13:33
vue-router之解决addRoutes使用遇到的坑
2024-04-30 10:19:26
Centos5.x下升级python到python2.7版本教程
2023-04-15 13:50:07
松子学ASP—Server篇
2008-04-22 14:14:00
Python matplotlib seaborn绘图教程详解
2021-03-12 03:23:07
![](https://img.aspxhome.com/file/2023/6/109866_0s.png)
PHP设计模式中观察者模式详解
2023-05-27 04:43:31
python区块链创建多个交易教程
2021-05-28 13:40:42