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

yipeiwu_com5年前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检查和同步本地时间(北京时间)的实现方法

背景 有时本地服务器的时间不准了,需要同步互联网上的时间。 解决方案 NTP时间同步,找到一些可用的NTP服务器进行同步即可。 通过获取一些大型网站的时间来同步为自己的时间...

python读取TXT每行,并存到LIST中的方法

python读取TXT每行,并存到LIST中的方法

文本如图: Python: import sys result=[] with open('accounts.txt','r') as f: for line in f: re...

Python 函数list&read&seek详解

Python 函数list&read&seek详解

一、函数list (1)定义:用打开的文件作为参数,把文件内的每一行内容作为一个元素 (2)格式:list(文件) (3)例子: with open(r"test01.txt",'r...

浅析Python中的for 循环

Python for 和其他语言一样,也可以用来循环遍历对象,本文章向大家介绍Python for 循环的使用方法和实例,需要的朋友可与参考一下。 一个循环是一个结构,导致第一个程序要重...

解决python tkinter界面卡死的问题

解决python tkinter界面卡死的问题

如果点击按钮,运行了一个比较耗时的操作,那么界面会卡死。 import tkinter as tk import time def onclick(text, i): tim...