Python 使用 uiautomation 获取通讯录好友信息

Python 使用 uiautomation 获取通讯录好友信息

# coding:utf-8
import uiautomation as auto
import time
import pandas as pd
wechatWindow = auto.WindowControl(searchDepth=1,Name='微信',className='WeChatMainWndForPC')
button = wechatWindow.ButtonControl(Name='通讯录')
button.Click()
wechatWindow.SetActive()
contents = wechatWindow.ListControl(Name="联系人")
# button = wechatWindow.ButtonControl(Name='***'
result = []
def deli():

    threeLays = wechatWindow.GetLastChildControl().GetLastChildControl().GetLastChildControl()
    tag = wechatWindow.EditControl(searchDepth=10, foundIndex=2)
    tmp = tag.GetParentControl().GetParentControl()
    nikeName = tag.Name
    items = tmp.GetParentControl().GetNextSiblingControl().GetNextSiblingControl().GetChildren()
    a1 = []
    row = {'微信号': '', '地址': ''}
    for item in items:
        try:

            line = item.GetChildren()
            # print(line[0].Name)
            de = line[1].Name.split(" ")
            # print(de)
            a1.append(line[1].Name)
            # print(")))))))))")
        except Exception as e:
            print(e)
        # print(de)
    # print(a1)
    row['微信号'] = a1[2]
    row['地址'] = a1[1]
    row['备注'] = a1[0]
    result.append(row)


# button.Click()

# print(wechatWindow.GetLastChildControl().GetLastChildControl().GetChildren()[1:2])
# print(contents.GetChildren()[8].Name)

# a = wechatWindow.ButtonControl(Name='ContactListItem')
# a.Click()
# a.SendKeys('{PAGEDOWN}')
i = 8
j = 0
while 1:
    if j == 0:
        for list1 in range(8,len(contents.GetChildren())):
            contents.GetChildren()[list1].Click()
            # print(contents.GetChildren()[list1].Name)
            deli()
    else:
        for list1 in range(1,len(contents.GetChildren())):
            contents.GetChildren()[list1].Click()
            # print(contents.GetChildren()[list1].Name)
            deli()

    print(len(contents.GetChildren()))
    if len(contents.GetChildren())>10:
        contents.GetChildren()[4].Click()
        time.sleep(1)
        contents.GetChildren()[4].SendKeys('{PAGEDOWN}')
    else:
        contents.GetChildren()[0].Click()
        time.sleep(1)
        contents.GetChildren()[0].SendKeys('{PAGEDOWN}')

    i = i+1
    j = j+1
    if i >9:
        break
"""
while 1 :
    c = contents.GetChildren()[i]
    c.Click()
    c.SendKeys('{PAGEDOWN}')
    print(c.Name)
    i=i+1
    if i> 20:
        break
"""
print(result)
dataTable = pd.DataFrame(result)
# print(dataTable)
dataTable.drop_duplicates(subset=['微信号'],keep='first',inplace=True)
# print(dataTable)
dataTable.to_excel("123.xlsx",sheet_name='微信通讯录好友备份',encoding='gb2312',columns=['微信号','地址','备注'])

参考 https://blog.csdn.net/weixin_43933035/article/details/122842002?utm_term=uiautomation%E5%BE%AE%E4%BF%A1%E9%80%9A%E8%AE%AF%E5%BD%95

您可能还喜欢...

发表回复