20220719 uiautomation之二三
今天在学习利用uiautomation库进行微信数据爬取,参考链接:https://blog.csdn.net/MicalChen/article/details/120296341。
参考项目:https://github.com/cluic/wxauto/blob/c3d31828288acb3925d4fd3f5aa09c5f72e11731/wxauto.py
具体uia的使用说明比较详细的介绍可以参考:https://blog.csdn.net/knighthood2001/article/details/124905213。
1、
先学习uiautomation具体用法,应该类同于pywinauto库。看了下基本的读取窗口控件之类的一样。看网上相关的获取微信通讯录的好友昵称与备注的功能,用uiautomation实现的(参考链接:https://www.ape-ask.com/read-127844.html)。
# -*- coding: utf-8-*- import subprocess import uiautomation as auto import time import pandas as pd df = [] columns = ['序号', '昵称', '备注', '标签'] # excel的标题栏 # ToDo 这里需要更改微信所在地址,可通过桌面微信图标右键打开文件所在的位置查找到路径 subprocess.Popen('E:\微信\WeChat\WeChat.exe') wechatWindow = auto.WindowControl(searchDepth=1, className='WeChatMainWndForPC', Name='微信') # 点击通讯录 button = wechatWindow.ButtonControl(Name='通讯录') button.Click() # 点击通讯录管理 administration = wechatWindow.ButtonControl(Name="通讯录管理") administration.Click() communication_administration = auto.WindowControl(Name="通讯录管理", ClassName="ContactManagerWindow") # 将鼠标放至内容的中心,滚轮对联系人列表才能生效 communication_administration.MoveCursorToMyCenter() list1 = communication_administration.ListControl(Name="") # a表示存储列表,b表示计数器 a = [] b = 1 flag = True start_time = time.time() print("""开始时间:{}""".format(start_time)) while flag: list1 = communication_administration.ListControl(Name="") # nickname = list1.GetChildren()[0].TextControl() nickname = list1.GetChildren()[0].GetFirstChildControl().GetFirstChildControl().GetChildren()[1].GetChildren()[1] beizhu = list1.GetChildren()[0].GetFirstChildControl().GetFirstChildControl().GetChildren()[2].ButtonControl() biaoqian = list1.GetChildren()[0].GetFirstChildControl().GetFirstChildControl().GetChildren()[3].ButtonControl() # print(nickname.Name) # print(beizhu.Name) # print(biaoqian.Name) '''判断是不是在列表a中,如果''' if len(a) == 0 or nickname.Name != a[-1]: # print(nickname.Name) # print(beizhu.Name) # print(biaoqian.Name) # print("="*50) print(b, '', nickname.Name) df.append([b, nickname.Name, beizhu.Name, biaoqian.Name]) d = pd.DataFrame(df, columns=columns) d.to_excel("微信朋友信息.xlsx", index=False) b += 1 a.append(nickname.Name) # 滚轮下滚 auto.WheelDown(waitTime=0.01) # 手动实现滚轮滚动到底操作 # 空格 if auto.IsKeyPressed(auto.Keys.VK_SPACE): print("到底了") for j in list1.GetChildren()[1:]: last_nickname = j.TextControl() if last_nickname != a[-1]: print(b, '', last_nickname.Name) last_beizhu = j.GetFirstChildControl().GetFirstChildControl().GetChildren()[2].ButtonControl() last_biaoqian = j.GetFirstChildControl().GetFirstChildControl().GetChildren()[3].ButtonControl() df.append([b, last_nickname.Name, last_beizhu.Name, last_biaoqian.Name]) d = pd.DataFrame(df, columns=columns) d.to_excel("微信朋友信息.xlsx", index=False) b += 1 a.append(last_nickname) print(a) flag = False end_time = time.time() sum_time = end_time - start_time print("""运行时间:{}s""".format(sum_time))
另有一篇文章,用uiautomation自动隐藏显示微信窗口的功能,可参考链接:https://blog.csdn.net/weixin_43090804/article/details/122327308。