使用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

相關文章
相關標籤/搜索