python itchat给指定联系人发消息的方法

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

python itchat给指定联系人发消息的方法

  2021-04-03 我要评论

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方法,就可以成功向老王发送消息了

您可能感兴趣的文章:

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们