使用itchat可以简单操作微信,进行好友和群消息的发送

安装:

pip install itchat

使用:

import itchat, time

# 登录
itchat.auto_login(hotReload=True) def send_one_group(msg, g_name):
"""
给某个组发送消息
:param msg:
:param g_name:
:return:
"""
rooms = itchat.search_chatrooms(g_name)
if rooms is not None:
user_name = rooms[]['UserName']
itchat.send(msg, toUserName=user_name)
else:
print("None group found") def send_all_group(msg):
"""
给所有组发送消息
:param msg:
:return:
"""
rooms = itchat.get_chatrooms(update=True)
if rooms is not None:
for r in rooms:
user_name = r['UserName']
itchat.send(msg, toUserName=user_name)
else:
print("None group found") def send_one_person(msg, p_name):
"""
给某个人发消息
:param msg:
:param p_name:
:return:
"""
persons = itchat.search_friends(p_name)
if persons is not None:
user_name = persons[]['UserName']
itchat.send_msg(msg, toUserName=user_name)
else:
print("None person found") def send_all_person(msg):
"""
给所有人发消息
:param msg:
:return:
"""
persons = itchat.get_friends()
if persons is not None:
for p in persons:
user_name = p['UserName']
itchat.send(msg, toUserName=user_name)
else:
print("None person found") if __name__ == '__main__':
send_one_person('测试123', "安")
# send_one_group('','***学员群')

小实例:

获取所有头像并保存和拼接到一个图片里面:

# -*- coding:utf- -*-
# 导入相关模块
import itchat
import os
import PIL.Image as Image
from os import listdir
import math # 登录
itchat.auto_login(hotReload=True)
# 获取微信全部好友的信息
friends = itchat.get_friends(update=True)[:]
# 获取自己的用户名
user = friends[]["UserName"]
# 打印用户名
print(user)
# 建立文件夹用来装好友的头像
os.mkdir(user) num =
# 遍历好友信息,将头像保存
for i in friends:
img = itchat.get_head_img(userName=i["UserName"])
fileImage = open(user + "/" + str(num) + ".jpg", 'wb')
fileImage.write(img)
fileImage.close()
num += pics = listdir(user)
numPic = len(pics)
print(numPic)
eachsize = int(math.sqrt(float( * ) / numPic))
print(eachsize)
numline = int( / eachsize)
toImage = Image.new('RGBA', (, ))
print(numline) x =
y = for i in pics:
try:
# 打开图片
img = Image.open(user + "/" + i)
except IOError:
print("Error: 没有找到文件或读取文件失败")
else:
# 缩小图片
img = img.resize((eachsize, eachsize), Image.ANTIALIAS)
# 拼接图片
toImage.paste(img, (x * eachsize, y * eachsize))
x +=
if x == numline:
x =
y += # 保存拼接后的头像
toImage.save(user + ".BMP")
itchat.send_image(user + ".BMP", 'filehelper')

python itchat 微信开发的更多相关文章

  1. python之-微信开发学习

    微信公众平台技术文档https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1445241432# 注意,最好以python3 运行,中文 ...

  2. python实现微信接口(itchat)

    python实现微信接口(itchat) 安装 sudo pip install itchat 登录 itchat.auto_login() 这种方法将会通过微信扫描二维码登录,但是这种登录的方式确实 ...

  3. 细数Python Flask微信公众号开发中遇到的那些坑

    最近两三个月的时间,断断续续边学边做完成了一个微信公众号页面的开发工作.这是一个快递系统,主要功能有用户管理.寄收件地址管理.用户下单,订单管理,订单查询及一些宣传页面等.本文主要细数下开发过程中遇到 ...

  4. python实现微信接口——itchat模块

    python实现微信接口——itchat模块 安装 sudo pip install itchat 登录 itchat.auto_login()  这种方法将会通过微信扫描二维码登录,但是这种登录的方 ...

  5. Python itchat模块的使用,利用图灵机器人进行微信消息自动回复

    一.下载安装itchat模块 二.小实验:获取微信好友头像信息 这需要用itchat模块中的一个方法 itchat.get_friends()#获取微信所有微信好友信息 现在我们导入itchat,打印 ...

  6. python 全栈开发,Day25(复习,序列化模块json,pickle,shelve,hashlib模块)

    一.复习 反射 必须会 必须能看懂 必须知道在哪儿用 hasattr getattr setattr delattr内置方法 必须能看懂 能用尽量用__len__ len(obj)的结果依赖于obj. ...

  7. 利用Python统计微信联系人男女比例以及简单的地区分布

    寒暄的话不多说,直接进入主题. 运行效果图: [准备环境] Python版本:v3.5及其以上 开发工具:随意,此处使用Pycharm [依赖包] 1.itchat (CMD运行:pip instal ...

  8. 用python玩微信(聊天机器人,好友信息统计)

    1.用 Python 实现微信好友性别及位置信息统计 这里使用的python3+wxpy库+Anaconda(Spyder)开发.如果你想对wxpy有更深的了解请查看:wxpy: 用 Python 玩 ...

  9. python发送微信及企业微信消息

    1.发送微信消息 直接使用第三方库 itchat,其文档中有详细使用方式:https://itchat.readthedocs.io/zh/latest/ 如下实例为 发送群聊信息 # -*- cod ...

随机推荐

  1. nodejs的process.on('message')转promise

    // main.js const cp = require('child_process'); const child = cp.fork('./child.js'); const callbackP ...

  2. android 捕获未try的异常

    1.Thread.UncaughtExceptionHandler java里有很多异常如:空指针异常,越界异常,数值转换异常,除0异常,数据库异常等等.如果自己没有try / catch 那么线程就 ...

  3. How to Deinstall Oracle Clusterware Home Manually

    ###sample 0:安装GI 和DB soft 都成功,如何回退DB soft [opdb@pdbdb01:/db/db/app/db/product/11204/deinstall]$ ./de ...

  4. Vue生命周期钩子详解【个人解读】

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. Django项目使用七牛云存储图片

    Django项目使用七牛云存储图片 最近,写了一个django项目,想在项目中使用七牛云存储上传图片,在网上搜索到django-qiniu-storage,查看文档,按步骤居然设置成功了. 安装 1 ...

  6. hive Tutorial

    hive数据单元按照粒度从大到小,依次为 1.数据库database:可以用show databases; 命令查看所有的数据库,并用use d1; 命令来选中d1数据库,接下来就可以操作d1数据库中 ...

  7. oracle--dump & V$BH

    一,什么是BH BH即Buffer Header,每一个数据块在被读入buffer cache时,都会先在buffer cache中构造一个buffer header,buffer header与数据 ...

  8. MySQL子查询subquery

    子查询(Subquery)是指出现在其他SQL语句内的SELECT子句. 例如: select * from t1 where col1=(select col2 from t2); 其中select ...

  9. linux系统下图片的路径

    1. 图片跟网页或者程序在同一目录下 直接 src="abc.jpg" 如果不行 就加多一个斜杠 src="/abc.jpg"

  10. Eclipse更改颜色主题

    通过在线安装的方式 Help -> Install New Software Work with: 输入 http://eclipse-color-theme.github.com/update ...