python获取微信所有群列表

获取微信群列表,但是这个方法首次获取的时候会获取到公众号,有改进的方法望告知

import uiautomation as auto
import re

wechatWindow = auto.WindowControl(
    searchDepth=1, className="WeChatMainWndForPC", Name="微信"
)
auto.SendKeys(text="{Alt}{Ctrl}w")

# 点击通讯录
button = wechatWindow.ButtonControl(Name="通讯录")
button.Click()

MainControl1 = [i for i in wechatWindow.GetChildren() if not i.ClassName][0]
MainControl2 = MainControl1.GetFirstChildControl()
SessionBox = MainControl2.GetChildren()


def find_group():
    """
    获取群聊
    """

    # 回到列表顶部
    while True:
        items = SessionBox[1].ListControl().GetChildren()
        if items and items[0].ControlTypeName == "PaneControl":
            SessionBox[1].WheelUp(wheelTimes=1, interval=0)
            break
        SessionBox[1].WheelUp(wheelTimes=5, interval=0)

    group_list = []
    pattern = "^[A-Z]$|^#$"
    is_break = False  # 是否跳出循环
    is_group = False  # 是否出现群聊

    while True:
        items = SessionBox[1].ListControl().GetChildren()

        for item in items:
            if item.TextControl().Exists():
                if item.TextControl().Name == "群聊" and item.Name == "":
                    is_group = True

                # 出现字母则退出循环
                if re.match(pattern, item.TextControl().Name) and item.Name == "":
                    is_break = True
                    break
                elif is_group and not (
                    item.TextControl().Name == "群聊" and item.Name == ""
                ):
                    group_list.append(item.Name)

        if is_break:
            break

        SessionBox[1].WheelDown(wheelTimes=4, interval=0)

    unique_list = []
    seen = set()
    for item in group_list:
        if item not in seen:
            unique_list.append(item)
            seen.add(item)

    print("\nunique_list\n", unique_list, len(unique_list))


find_group()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值