Python uiautomation 是一個(gè)用于自動(dòng)化 GUI 測(cè)試和操作的庫(kù),它可以模擬用戶操作來(lái)執(zhí)行各種任務(wù)。
通過(guò)這個(gè)庫(kù),可以使用Python腳本模擬人工點(diǎn)擊,人工操作界面。本文使用 Python uiautomation 進(jìn)行微信電腦版的操作。
以下是本次實(shí)驗(yàn)的版本號(hào)。
你需要安裝 uiautomation
pip install uiautomation
示例代碼
import time
import uiautomation as auto
import re
from plyer import notification
notification_history = {} # 歷史消息
def check_wechat_messages():
# 獲取微信窗口
wechat_win = auto.WindowControl(Name="微信", ClassName="WeChatMainWndForPC")
shoukuanWin = wechat_win.ListControl(Name="會(huì)話")
bbb = shoukuanWin.GetChildren()
for chatMsg in bbb:
if "條新消息" in chatMsg.Name:
# 計(jì)算消息條數(shù)
match = re.match(r'([a-zA-Z0-9]+)(\d+)條新消息', chatMsg.Name)
if match:
nickname = match.group(1)
message_count = int(match.group(2))
printInfo = f"{nickname} 給你發(fā)送了 {message_count} 條消息"
print(printInfo)
print("------------")
# 獲取消息列表控件
xiaoxis = wechat_win.ListControl(Name="消息")
# 獲取消息列表控件的子控件
xiaoxi_children = xiaoxis.GetChildren()
# 獲取最后一個(gè)子控件
last_xiaoxi = xiaoxi_children[-1]
# 打印最后一條消息的內(nèi)容
print(last_xiaoxi.Name)
# 在指定時(shí)間內(nèi)不重發(fā)
last_notification_time = notification_history.get((nickname, message_count), 0)
current_time = time.time()
if current_time - last_notification_time > 15:
# 依次發(fā)送
notification_title = f"來(lái)自 {nickname} 的 {message_count} 條消息"
notification_message = f"{last_xiaoxi.Name}"
notification.notify(
title=notification_title,
message=notification_message,
app_name="WeChat"
)
# 更新日志
notification_history[(nickname, message_count)] = current_time
if __name__ == "__main__":
try:
while True:
check_wechat_messages()
time.sleep(2) #2秒檢測(cè)一次UI組件
except KeyboardInterrupt:
print("程序退出~")
except Exception as e:
print(f"程序執(zhí)行出現(xiàn)了問(wèn)題: {str(e)}")
代碼解析:
以上代碼使用?uiautomation?實(shí)時(shí)獲取微信聊天列表的消息狀態(tài),一旦有消息發(fā)過(guò)來(lái),就會(huì)獲取到發(fā)送人的微信昵稱以及發(fā)送的消息內(nèi)容、消息個(gè)數(shù)。
每2秒獲取一次UI控件的內(nèi)容,實(shí)測(cè)掛在后臺(tái)對(duì)CPU和內(nèi)存占用并無(wú)明顯影響,結(jié)合Python uiautomation的各種用法,可以做成自動(dòng)回復(fù)的功能。使用這款軟件,可以獲取到微信電腦版大部分控件的內(nèi)容。例如:微信聊天列表、群名稱、好友微信昵稱、群人數(shù)、微信號(hào)等。?
還可以獲取到群內(nèi)的每一條聊天內(nèi)容,獲取到你跟好友的聊天記錄。?
只要?UISpy.exe?可獲取到的控件內(nèi)容,那么你用 Python就可以獲取到。
拓展
還可以用來(lái)做收款監(jiān)控。將【微信收款助手】這個(gè)公眾號(hào)單獨(dú)窗口出來(lái),然后監(jiān)控這個(gè)窗口。
妥妥的實(shí)現(xiàn)了一個(gè)PC收款監(jiān)控??梢杂脕?lái)做收款碼的支付回調(diào)。
import uiautomation as auto
import re
import time
def get_children_at_depth(control, target_depth, current_depth=0):
children = control.GetChildren()
result = []
for child in children:
if current_depth == target_depth:
result.append(child)
else:
result.extend(get_children_at_depth(child, target_depth, current_depth + 1))
return result
def process_last_child_information(previous_info):
weixin = auto.WindowControl(Name="微信收款助手", ClassName="ChatWnd")
xiaoxi = weixin.ListControl(Name="消息")
target_depth = 5
depth_5_children = get_children_at_depth(xiaoxi, target_depth)
# 正則表達(dá)式模式
pattern = r'收款到賬通知(\d+月\d+日 \d+:\d+)收款金額¥([0-9.]+)匯總'
last_child = None
for child in depth_5_children:
match = re.search(pattern, child.Name)
if match:
last_child = child # 保存最后一條子控件的引用
# 在循環(huán)結(jié)束后,提取最后一條子控件的信息
if last_child:
match = re.search(pattern, last_child.Name)
if match:
date_time = match.group(1)
amount = match.group(2)
# 監(jiān)聽(tīng)下一筆
if (date_time, amount) != previous_info:
print("收款回調(diào):")
print(date_time)
print("金額:", amount)
print("正在等待下一筆...")
print("----------")
previous_info = (date_time, amount)
return previous_info
# 循環(huán)
previous_info = None
while True:
previous_info = process_last_child_information(previous_info)
# 每2秒執(zhí)行一次循環(huán)
time.sleep(2)
請(qǐng)勿使用這種技術(shù)用于非法行為,僅供大家開(kāi)發(fā)一寫小工具自己用。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-763599.html
如果使用這種技術(shù)來(lái)進(jìn)行違法行為所帶來(lái)的責(zé)任自行負(fù)責(zé),與工具、教程作者、發(fā)布的平臺(tái)無(wú)關(guān)。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-763599.html
到了這里,關(guān)于Python uiautomation獲取微信內(nèi)容!聊天記錄、聊天列表、全都可獲取的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!