微信
python原创微信自动化运维大约 2 分钟约 625 字
获取微信好友
# -*- coding: utf-8-*-
import subprocess
import uiautomation as auto
import time
# ToDo 这里需要更改微信所在地址,可通过桌面微信图标右键打开文件所在的位置查找到路径
# subprocess.Popen('E:微信WeChatWeChat.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表示计数器,flag控制代码是否退出
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()
'''判断是不是在列表a中,如果列表中没有,则添加'''
if nickname.Name not in a:
print(b, '', nickname.Name)
b += 1
a.append(nickname.Name)
with open('name.txt', 'a', encoding='utf-8') as f:
f.write(nickname.Name)
f.write('')
# 滚轮下滚
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 not in a:
print(b, '', last_nickname.Name)
b += 1
a.append(last_nickname)
with open('name.txt', 'a', encoding='utf-8') as f:
f.write(last_nickname.Name)
f.write('')
print(a)
flag = False
end_time = time.time()
sum_time = end_time - start_time
print("""运行时间:{}s""".format(sum_time))
微信好友发送消息
# -*- coding: utf-8-*-
import subprocess
import uiautomation as auto
import time
# ToDo 这里需要更改微信所在地址,可通过桌面微信图标右键打开文件所在的位置查找到路径
# subprocess.Popen('E:微信WeChatWeChat.exe')
wechatWindow = auto.WindowControl(searchDepth=1, className='WeChatMainWndForPC', Name='微信')
wechatWindow.SwitchToThisWindow()
# 点击通讯录
button = wechatWindow.ButtonControl(Name='通讯录')
button.Click()
search = wechatWindow.EditControl(Name='搜索')
search.Click()
search.GetParentControl().GetChildren()[1].SendKeys("寒颜")
searResult = wechatWindow.ListControl(Name='搜索结果').GetChildren()
for s in searResult:
if(s.Name=="寒颜灬冷若霜"):
s.Click()
break
wechatWindow.EditControl(Name="输入").SendKeys("hello")
wechatWindow.SendKeys("{Enter}")
获取微信好友消息
session=wechatWindow.ListControl(Name="消息")
for s in session.GetChildren():
print(s.Name)
微信聊天列表打开
for s in wechatWindow.ListControl().GetChildren():
if(s.Name.startswith('小悦')):
s.Click()
获取微信通讯录
from wxauto import *
import uiautomation as ui
wx = WeChat()
wx.UiaAPI.SwitchToThisWindow()
wx.UiaAPI.ButtonControl(Name='通讯录').Click()
wx.UiaAPI.ButtonControl(Name='通讯录管理').Click()
contact = ui.WindowControl(Name="通讯录管理", ClassName="ContactManagerWindow")
contact.MoveCursorToMyCenter()
contactList = []
lastList = []
flag = True
i = 1
ele=contact.ListControl(Name='').GetChildren()[1].PaneControl().PaneControl()
# 获取昵称、备注、标签
#print(ele.GetChildren()[1].TextControl())
#print(ele.GetChildren()[2].ButtonControl())
#print(ele.GetChildren()[3].PaneControl().ButtonControl())
while flag:
list = []
for s in contact.ListControl(Name="").GetChildren():
nickname = s.TextControl().Name
if nickname not in contactList:
list.append(nickname)
contactList.append(nickname)
contact.WheelDown(wheelTimes=8, waitTime=0.1 * i)
flag = [False for c in list if c not in lastList]
i += 1
if i > 30:
flag = False
lastList = list
ui.ButtonControl(Name='关闭').Click()
print(contactList)