需求如下

班级微信群需要每天上报由每个家长发送的健康码, 现在需要将微信群中家长发送的图片(健康码) 保存为孩子姓名(微信群里 家长群备注去掉后两位,如马云爸爸,去掉后两位,保存为马云1.jpg、马云2.jpg), 然后将所有图片保存到以当天日期命名的文件夹。

可以调用wxpy模块

实现

wx.py

from wxpy import *
import time,os

# 微信机器人,缓存登录信息
# 如果你需要部署在服务器中,则在下面加入一个入参console_qr=True
# console_qr表示在控制台打出二维码,部署到服务器时需要加上
bot = Bot(cache_path=True)

# 当前日期文件夹
path = time.strftime("%Y%m%d", time.localtime())
path = f'D:\\{path}\\' 
if not os.path.isdir(path):
    os.makedirs(path)

# 监控群聊
listen_groups = '群'  
bot.listen_groups = bot.groups().search(listen_groups)
if len(bot.listen_groups) < 1:
    bot.listen_groups = []
    print(f'未找到群名包含「{listen_groups}」的群聊!')
else:
    print(f'找到群名包含「{listen_groups}」的群聊{str(len(bot.listen_groups))}个!')

def pfn(fn,num=1):
    if not os.path.isfile(fn):
        return fn
    else:
        fnlist = fn.split('.')
        return pfn(fn=f'{"".join(fnlist[0:-1])}{str(num)}.{fnlist[-1]}',num=num+1)
"""群功能"""
@bot.register(chats=Group)
def group_msg(msg):
    """接收群消息"""

    # 监控群聊中的图片
    if msg.chat in msg.bot.listen_groups and msg.type == PICTURE :
        fn = pfn(f'{path}{msg.member.name[0:-2]}.{msg.file_name.split(".")[-1]}')
        print(fn)
        msg.get_file(fn)
    else:
        pass

    return None

bot.join()

简单的测试

python wxpy将微信群聊中的图片保存到本地
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。