环境:PyCharm Community Edition 2022.3.2、Python3.8、微信版本信息 3.9.9.43
PS:需外界显示器将微信界面放到外置显示器上,不然会打不开微信窗口报错
import uiautomation as auto
wechatWindow = auto.WindowControl(searchDepth=1, className='WeChatMainWndForPC', Name='微信')
# 点击通讯录
button1 = wechatWindow.ButtonControl(Name='通讯录')
button1.Click()
# 点击通讯录管理
button2 = wechatWindow.ButtonControl(Name="通讯录管理")
button2.Click()
communication_administration = auto.WindowControl(Name="通讯录管理", ClassName="ContactManagerWindow")
print('打开通讯录窗口')
wechatWindow2 = auto.WindowControl(searchDepth=1, className='WeChatMainWndForPC', Name='通讯录管理')
print('定位通讯录管理')
communication_administration.MoveCursorToMyCenter()
communication_administration.MoveCursorToMyCenter()
#每次获取的好友信息写到列表
list_contact = []
#所有的好友信息写到列表
list_sum = []
#定义最后一位好友出现的次数,以终止循环
count = 0
#获取全部的好友列表
while count <= 10:
for name_node in wechatWindow2.ListControl().GetChildren():
nc = name_node.TextControl().Name
bz = name_node.ButtonControl(foundIndex=2).Name
if bz == "添加备注":
bz = ""
bq = name_node.ButtonControl(foundIndex=3).Name
if bq == "添加标签":
bq = ""
auto.WheelDown(wheelTimes=1, waitTime=0.01, interval=0.01)
list_contact = [nc,bz,bq]
list_sum.append(list_contact)
last_element = list_sum[-1] # 获取列表中的最后一个元素
count = list_sum.count(last_element) # 统计最后一个元素出现的次数
#去除重复获取到的好友
unique_list = [list(item) for item in set(tuple(row) for row in list_sum)]
#打印全部好友
print(unique_list)
print(len(unique_list))