【uiautomation】微信群发消息,获取群通讯录名单
前言
接到了一个需求:现微信有8000+好友,需要给所有好友发送一则一样的消息。网上搜索一番后,发现
uiautomation
可以解决该需求,遂有此文。这是第四篇,获取群聊通讯录的用户名单。
代码在文章末尾,自取~
更多功能的微信群发消息代码链接 :https://github.com/Frica01/Wechat_mass_msg
知识点📖
知识点 | 链接 |
---|---|
Microsoft 的 uiautomation | https://docs.microsoft.com/zh-cn/dotnet/framework/ui-automation/ui-automation-overview |
Python 的 uiautomation | https://github.com/yinkaisheng/Python-UIAutomation-for-Windows |
微信群发消息 GitHub链接 | https://github.com/Frica01/Wechat_mass_msg |
代码实现
该方法主要实现获取群聊通讯录口中的用户名称。
借助 inspect.exe
工具,可以清楚的看到对应的节点。而在知道节点后,后面的工作就很简单了!!
聊天信息窗口
聊天成员窗口
代码如下:
运行效果如下动图所示:
截取部分返回的名单片段,如下:
[
'局长星仔', '飞', 'Sad.', '几好君', 'Tt', '๑YANYANYAN๑', '3333', '🚲', '゛🍭 ShAdoW♪ ℡', '绿树森林', '条条🐳',
'LUCKY🎁', '哈路', '二晓🐒', '杏仁草', '🌈桐楠🌈', 'Renee', '宁', 'SK🐑', 'Pinky', '亍亍', '龙儿', 'Air°', '🎀梁小诗🎀', '叶叶叶子',
'☝DvDv', '🐾 Yang.駿💮', 'Ni', 'sandra', 'Ellie🐳', 'tracy', '李剑淳', '🌻', ' 森さん🎄',
...
]
由上可见,有部分是用户名称中包含Emoji
表情的,也可以拿下来~
完整代码
# -*- coding: utf-8 -*-
# @Author : Frica01
# @Time : 2022-09-10 15:39
# @Name : wechat_operation.py
import uiautomation as auto
wx_window = auto.WindowControl(Name='微信', ClassName='WeChatMainWndForPC')
auto.SendKeys(text='{Alt}{Ctrl}w') # 快捷键唤醒微信
def get_group_chat_name_list() -> list | None:
"""获取群聊通讯录中的用户姓名"""
name_list = list()
auto.ButtonControl(Name='聊天信息').Click()
chat_members_win = wx_window.ListControl(Name='聊天成员')
if not chat_members_win.Exists():
return
wx_window.ButtonControl(Name='查看更多').Click()
for idx, item in enumerate(chat_members_win.GetChildren()):
name = item.ButtonControl().Name
name_list.append(name)
return name_list
if __name__ == '__main__':
data = get_group_chat_name_list()
print(len(data))
print(data)
后话
如果看不懂代码,可以在下方留言~
see you.🎈🎈