使用python UIAutomation从QQ2017(v8.9)群界面获取所有群成员详细资料,

首先安装pip install uiautomation, 再运行本文代码。或者下载https://github.com/yinkaisheng/Python-UIAutomation-for-Windows代码(包含了uiautomation module),直接运行demos目录里的脚本get_qq_group_members.py

uiautomation.py是我写的一个python封装微软UIAutomation API的一个module,使用非常简单
先看我之前一篇文章介绍如何使用 https://www.cnblogs.com/Yinkaisheng/p/3444132.html

首先打开qq群聊天窗口,运行automation.py -a,然后3秒内移动鼠标到qq群上其中一个成员上面(下图右下角红框中),等待打印qq群窗口信息,
可以看到qq群窗口的控件树形结构。


再根据控件结构获取信息,只需60几行代码,如下:

#!python3
# -*- coding: utf-8 -*-
"""
本脚本可以获取QQ2017(v8.9.4)群所有成员详细资料,请根据提示做对应的操作
作者:yinkaisheng@foxmail.com
"""
import time
import uiautomation as automation


def GetPersonDetail():
    detailWindow = automation.WindowControl(searchDepth= 1, ClassName = 'TXGuiFoundation', SubName = '的资料')
    details = ''
    for control, depth in automation.WalkControl(detailWindow):
        if isinstance(control, automation.EditControl):
            details += control.Name + control.CurrentValue() + ' '
    details += ' ' * 2
    detailWindow.Click(-10, 10)
    return details


def main():
    automation.Logger.WriteLine('请把鼠标放在QQ群聊天窗口中的一个成员上面,3秒后获取 ')
    time.sleep(3)
    listItem = automation.ControlFromCursor()
    if listItem.ControlType != automation.ControlType.ListItemControl:
        automation.Logger.WriteLine('没有放在群成员上面,程序退出!')
        return
    consoleWindow = automation.GetConsoleWindow()
    if consoleWindow:
        consoleWindow.SetActive()
    qqWindow = listItem.GetTopWindow()
    list = listItem.GetParentControl()
    allListItems = list.GetChildren()
    for listItem in allListItems:
        automation.Logger.WriteLine(listItem.Name)
    answer = input('是否获取详细信息?按y和Enter继续 ')
    if answer.lower() == 'y':
        automation.Logger.WriteLine(' 3秒后开始获取QQ群成员详细资料,您可以一直按住F10键暂停脚本')
        time.sleep(3)
        qqWindow.SetActive()
        #确保群里第一个成员可见在最上面
        left, top, right, bottom = list.BoundingRectangle
        while allListItems[0].BoundingRectangle[1] < top:
            automation.Win32API.MouseClick(right - 5, top + 20)
        for listItem in allListItems:
            if listItem.ControlType == automation.ControlType.ListItemControl:
                if automation.Win32API.IsKeyPressed(automation.Keys.VK_F10):
                    if consoleWindow:
                        consoleWindow.SetActive()
                    input(' 您暂停了脚本,按Enter继续 ')
                    qqWindow.SetActive()
                listItem.RightClick()
                menu = automation.MenuControl(searchDepth= 1, ClassName = 'TXGuiFoundation')
                menuItems = menu.GetChildren()
                for menuItem in menuItems:
                    if menuItem.Name == '查看资料':
                        menuItem.Click()
                        break
                automation.Logger.WriteLine(listItem.Name, automation.ConsoleColor.Green)
                automation.Logger.WriteLine(GetPersonDetail())
                listItem.Click()
                automation.SendKeys('{Down}')

if __name__ == '__main__':
    main()
    input('press Enter to exit')

效果图

获取的到QQ群成员详细保存在脚本同一目录@AutomationLog.txt里

代码下载

 https://github.com/yinkaisheng/Python-UIAutomation-for-Windows

原文地址:https://www.cnblogs.com/Yinkaisheng/p/5114932.html