当前位置:   article > 正文

微信群成员的群昵称提取与批量查询(Python实现)_python wxauto 获取所有微信群名称

python wxauto 获取所有微信群名称

wechat_group_members

此脚本不存在任何联网上传机制,可安全使用

主要原理

通过pywinauto分析微信界面元素,提取群成员昵称和用户名

程序代码

from pywinauto.application import Application
import pywinauto
import time
import psutil
import pandas as pd
import numpy as np


def get_wechat_pid():
    pids = psutil.pids()
    for pid in pids:
        p = psutil.Process(pid)
        if p.name() == 'WeChat.exe':
            return pid
    return None


def get_name_list(pid):
    print('>>> WeChat.exe pid: {}'.format(pid))
    print('>>> 请打开【微信=>目标群聊=>聊天成员=>查看更多】,尤其是【查看更多】,否则查找不全!')
    for i in range(20):
        print('\r({:2d} 秒)'.format(20 - i), end='')
        time.sleep(1)
    app = Application(backend='uia').connect(process=pid)
    win_main_Dialog = app.window(class_name='WeChatMainWndForPC')
    chat_list = win_main_Dialog.child_window(control_type='List', title='聊天成员')
    name_list = []
    all_members = []
    for i in chat_list.items():
        p = i.descendants()
        if p and len(p) > 5:
            if p[5].texts() and p[5].texts()[0].strip() != '' and (p[5].texts()[0].strip() != '添加' and p[5].texts()[0].strip() != '移出'):
                name_list.append(p[5].texts()[0].strip())
                all_members.append([p[5].texts()[0].strip(), p[3].texts()[0].strip()])
    pd.DataFrame(np.array(all_members)).to_csv('all_members.csv', header=['群昵称', '微信昵称'])
    print('\r>>> 群成员共 {} 人,结果已保存至all_members.csv'.format(len(name_list)))
    return name_list


def match():
    pid = get_wechat_pid()
    if pid == None:
        print('>>> 找不到WeChat.exe,请先打开WeChat.exe再运行此脚本!')
        return
    try:
        member_list = get_name_list(pid)
    except pywinauto.findwindows.ElementNotFoundError as e:
        print('\r>>> 未找到【聊天成员】窗口,程序终止!')
        print('>>> 若已开启【聊天成员】窗口但仍报错,请重启微信(原因:可能存在多个WeChat进程)')
        return
    mode = ''
    while True:
        mode = input('>>> 是否需要根据name_list.txt进行匹配? [y/n] ')
        if mode != 'y' and mode != 'n':
            print('>>> 请输入 y 或 n选择模式 [y/n] ')
        else:
            if mode == 'n':
                return
            else:
                break
    not_found = []
    count = 0
    with open('./name_list.txt', 'r', encoding='utf-8')as fp:
        for i in fp.readlines():
            i, result = i.strip(), False
            if i == '':
                continue
            count += 1
            for j in member_list:
                if i in j:
                    result = True
                    member_list.remove(j)
            if not result:
                not_found.append(i)
    if count == 0:
        print('>>> name_list.txt为空!请输入待查询成员群昵称!(每行一个群昵称)')
        return
    print('>>> 匹配失败列表:\n------------------------------')
    print('\n'.join(not_found))
    print('------------------------------')


if __name__ == '__main__':
    match()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85

使用说明

  1. 启动微信
  2. 命令行执行python member_match.py
  3. 根据提示打开【微信=>目标群聊=>聊天成员=>查看更多】,尤其是【查看更多】,否则查找不全!
  4. 等待读秒结束,all_members.csv即为所有群成员的群昵称和用户名
  5. 选择模式,y代表进行匹配(查询name_list.txt中给定成员是否存在),n代表不匹配直接结束
    1. 若选择y,则事先应该在py文件同级目录准备好name_list.txt,内容为待查询成员群昵称,每行为一个群昵称(注意不是用户昵称,否则需要修改代码)
  6. 输出结果

成功案例

在这里插入图片描述

运行错误案例
在这里插入图片描述
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/831628
推荐阅读
相关标签
  

闽ICP备14008679号