使用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. R 升级版本

    从3.4.1 升级到最新 懒得去官网下载 在R studio 执行一下代码 install.packages("installr") library(installr) updat ...

  2. .net将List序列转为Json字符串

    将List类型转化为Json,是我们平常开发时最常见的了.在使用中,有很多种方法,也可以使用. 第一种 第三方组件:Newtonsoft.Json.dll //转化成Json Newtonsoft.J ...

  3. Java_方法的定义以及分类

    什么叫方法? 方法也叫做函数,实现某个功能 方法分类: 系统提供的方法: 常用的系统提供的方法:如:nextInt() next() nextDouble() print println()..... ...

  4. (转)Http状态码301和302概念简单区别及企业应用案例

    Http状态码301和302的区别及企业应用案例 原文:http://blog.51cto.com/oldboy/1774260 1.什么是301重定向? 301重定向/跳转一般,表示本网页永久性转移 ...

  5. MySQL插入记录 insert

    一.insert insert tb_name [(col_name,......)] {values | value} ({expr | default } ....... ) , ( ... ) ...

  6. 用Gradle 构建你的android程序

    前言 android gradle 的插件终于把混淆代码的task集成进去了,加上最近,android studio 用的是gradle 来构建项目, 下定决心把android gralde 构建项目 ...

  7. 转 功能强大的shell:if条件语句

    原文 http://blog.zol.com.cn/2322/article_2321767.html 功能强大的shell:if条件语句 if语句测试条件,测试条件返回真(0)或假(1)后,可相应执 ...

  8. OpenGL初识

    OpenGL 概念 OpenGL提供的是一系列接口, 它是指一个规范, OpenGL规范严格规定了每个函数该如何执行, 以及它们的输出值, 具体的实现是由各个显示设备厂商, 它作为本地系统库直接运行在 ...

  9. ddddddeeeessssssttttrrrrrrooooooyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy

    我遥远的 POI 计划啊 https://loj.ac/problems/search?keyword=POI2011 atcoder 一套 动态 DP SAM 随便看 XSY 的题 UOJ Roun ...

  10. 有标号的DAG计数 III

    Description 给定一正整数n,对n个点有标号的有向无环图进行计数,这里加一个限制:此图必须是弱连通图.输出答案 mod 10007 的结果. Solution 弱连通图即把边变成无向之后成为 ...