純自動化的python實(shí)現(xiàn)模仿的是人的手動操作的過程,并不做后端接口等的實(shí)際操作。
通過模仿手動操作完成業(yè)務(wù)處理,這里使用到了兩個python的非標(biāo)準(zhǔn)模塊,分別是pyautogui和pyperclip模塊。
如果沒有安裝的話直接使用pip的方式安裝一下這兩個模塊。
pip install pyperclip -i https://pypi.tuna./simple
pip install pyautogui -i https://pypi.tuna./simple
安裝過程沒有其他比較曲折的操作,安裝完成后將我們需要的模塊導(dǎo)入到代碼塊中即可。
# Importing the pyautogui module and renaming it to gui.
import pyautogui as gui
# Importing the pyperclip module and renaming it to clip.
import pyperclip as clip
# Importing the time module and renaming it to t.
import time as t
本文代碼塊中的部分注釋是使用AI插件自動生成的,省去了我開發(fā)代碼塊時編寫注釋的過程。
由于實(shí)現(xiàn)過程比較簡單這里并沒有編寫對象類,直接在.py的文件中創(chuàng)建一個send_message()函數(shù)。
def send_message(fri_name=None, msgs=None):
"""
This function sends a message to a friend.
:param fri_name: The name of the friend you want to send the message to
:param msgs: The message you want to send
"""
gui.hotkey('ctrl', 'alt', 'w')
gui.hotkey('ctrl', 'f')
gui.copy(fri_name)
gui.hotkey('ctrl', 'v')
t.sleep(0.5)
gui.press('enter')
# 通過使用一系列的快捷鍵的操作,這個時候需要發(fā)消息的用戶聊天窗口已經(jīng)打開了
for msg in msgs:
clip.copy(msg)
clip.hotkey('ctrl', 'v')
clip.press('enter')
t.sleep(1)
這個時候通過一系列的模仿手動打開微信和復(fù)制粘貼的動作以及使用enter鍵發(fā)送消息等,一系列動作就操作完了。
因?yàn)閏trl+alt+w的快捷鍵是打開微信,我們也可以替換成打開其他應(yīng)用的快捷鍵,比如說QQ等。
最后,只需要傳入朋友昵稱參數(shù)和需要發(fā)送的消息列表就能實(shí)現(xiàn)自動化發(fā)消息的操作了。
send_message(fri_name='張三', msgs=['消息1', '消息2', '消息3', '消息4', '消息5', '消息6', '消息7'])