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

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

uiautomation.py是我写的一个python封装微软UIAutomation API的一个module,使用很是简单
先看我以前一篇文章介绍如何使用 http://www.javashuo.com/article/p-hdxwmzdb-ec.htmlpython

 

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


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

#!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() + '\n'
    details += '\n' * 2
    detailWindow.Click(-10, 10)
    return details


def main():
    automation.Logger.WriteLine('请把鼠标放在QQ群聊天窗口中的一个成员上面,3秒后获取\n')
    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继续\n')
    if answer.lower() == 'y':
        automation.Logger.WriteLine('\n3秒后开始获取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('\n您暂停了脚本,按Enter继续\n')
                    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')

效果图ui

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

代码下载code

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

相关文章
相关标签/搜索