首先安裝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() + '\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')
效果圖
獲取的到QQ群成員詳細保存在腳本同一目錄@AutomationLog.txt里
代碼下載
https://github.com/yinkaisheng/Python-UIAutomation-for-Windows