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

yipeiwu_com6年前Python基础

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

pytorch 修改预训练model实例

我就废话不多说了,直接上代码吧! class Net(nn.Module): def __init__(self , model): super(Net, self)._...

Python编写屏幕截图程序方法

正在编写的程序用的很多Windows下的操作,查了很多资料。看到剪切板的操作时,想起以前想要做的一个小程序,当时也没做,现在正好顺手写完。 功能:按printscreen键进行截图的时候...

python操作redis的方法

本文实例讲述了python操作redis的方法。分享给大家供大家参考。具体如下: #!/usr/bin/python #coding=utf-8 import redis class...

Python随机生成信用卡卡号的实现方法

本文实例讲述了Python随机生成信用卡卡号的实现方法。分享给大家供大家参考。具体分析如下: 这段Python代码根据信用卡卡号产生规则随机生成信用卡卡号,是可以通过验证的,仅供学习参考...

python生成特定分布数的实例

我就废话不多说了,直接上代码吧! from scipy.stats import binom, norm, beta, expon import numpy as np import...