纯自动化的python实现模仿的是人的手动操作的过程,并不做后端接口等的实际操作。
通过模仿手动操作完成业务处理,这里使用到了两个python的非标准模块,分别是pyautogui和pyperclip模块。
如果没有安装的话直接使用pip的方式安装一下这两个模块。
pip install pyperclip -i https://pypi.tuna./simple
pip install pyautogui -i https://pypi.tuna./simple
安装过程没有其他比较曲折的操作,安装完成后将我们需要的模块导入到代码块中即可。
# 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插件自动生成的,省去了我开发代码块时编写注释的过程。
由于实现过程比较简单这里并没有编写对象类,直接在.py的文件中创建一个send_message()函数。
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')
# 通过使用一系列的快捷键的操作,这个时候需要发消息的用户聊天窗口已经打开了
for msg in msgs:
clip.copy(msg)
clip.hotkey('ctrl', 'v')
clip.press('enter')
t.sleep(1)
这个时候通过一系列的模仿手动打开微信和复制粘贴的动作以及使用enter键发送消息等,一系列动作就操作完了。
因为ctrl+alt+w的快捷键是打开微信,我们也可以替换成打开其他应用的快捷键,比如说QQ等。
最后,只需要传入朋友昵称参数和需要发送的消息列表就能实现自动化发消息的操作了。
send_message(fri_name='张三', msgs=['消息1', '消息2', '消息3', '消息4', '消息5', '消息6', '消息7'])