Python利用itchat库向好友或者公众号发消息的实例

yipeiwu_com6年前Python基础

首先获得好友或者公众号的UserName

1. 获取好友UserName

#coding=utf8
import itchat
itchat.auto_login(hotReload=True)

#想给谁发信息,先查找到这个朋友,name后填微信备注即可,deepin测试成功
users = itchat.search_friends(name='')
#获取好友全部信息,返回一个列表,列表内是一个字典
print(users)
#获取`UserName`,用于发送消息
userName = users[0]['UserName']
itcha.send("hello",toUserName = userName)
#coding=utf8
import itchat
itchat.auto_login(hotReload=True) 
#获取所有好友信息
account=itchat.get_friends()
# #获取自己的UserName
userName = account[0]['UserName']

2. 获取公众号UserName

#coding=utf8
import itchat

itchat.auto_login(hotReload=True) 
#返回完整的公众号列表
mps = itchat.get_mps()
## 获取名字中含有特定字符的公众号,也就是按公众号名称查找,返回值为一个字典的列表
mps = itchat.search_mps(name='CSDN')
print(mps)
#发送方法和上面一样
userName = mps[0]['UserName']
itchat.send("hello",toUserName = userName)

3. 发送内容代码如下

#coding=utf8
import itchat

itchat.auto_login(hotReload=True) 
#获取通讯录信息
account=itchat.get_friends()
# #获取自己的UserName
userName = account[0]['UserName']
#获取公众号信息
# mps = itchat.get_mps()
# print(mps)
lines = []
#读取txt文件
f = open("/home/numb/Desktop/aaa.txt") 
lines = f.readlines()#读取全部内容 
#循环发送文本内容
for i in range(90): 
 #UserName需要用上面获取的自己修改
 itchat.send(lines[i],toUserName='UserName') 
print("Success")

以上这篇Python利用itchat库向好友或者公众号发消息的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python 寻找list中最大元素对应的索引方法

如下所示: aa = [1,2,3,4,5] aa.index(max(aa)) 如果aa是numpy数组: aa = numpy.array([1,2,3,4,5]) 先...

win10下Python3.6安装、配置以及pip安装包教程

win10下Python3.6安装、配置以及pip安装包教程

0.目录 1.前言 2.安装python 3.使用pip下载、安装包 3.1 安装Scrapy 3.2 安装PyQt 3.3 同时安装多个包 3.4 pip的常用命令 1.前言 之前在电...

Python中将两个或多个list合成一个list的方法小结

python中,list这种数据结构很常用到,如果两个或者多个list结构相同,内容类型相同,我们通常会将两个或者多个list合并成一个,这样我们再循环遍历的时候就可以一次性处理掉了。所...

python中将字典形式的数据循环插入Excel

python中将字典形式的数据循环插入Excel

1.我们看到字典形式的数据如下所示 list=[["2891-1", "D"],["2892-1", "D"],["2896-1", "B"],["2913-1", 0],["291...

Python实现字典排序、按照list中字典的某个key排序的方法示例

本文实例讲述了Python实现字典排序、按照list中字典的某个key排序的方法。分享给大家供大家参考,具体如下: 1.给字典按照value按照从大到小排序 排序 dict = {'...