获取微信群列表,但是这个方法首次获取的时候会获取到公众号,有改进的方法望告知
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()