1.前言
传统的手动操作在重复性任务中效率低下,且易受人为误差影响。而Python凭借其丰富的生态库(如pyautogui、pyperclip)和跨平台特性,成为自动化领域的首选语言。通过模拟鼠标点击、键盘输入等操作,脚本可以替代人工完成许多重复性工作场景。本文深度解析一款基于Python的智能预约脚本,其通过计算机视觉与GUI自动化技术,实现了微信小程序全流程无人值守预约。
2.脚本功能概述
自动化操作的智能引擎
附件中的脚本通过pyautogui与pyperclip库实现微信小程序自动化操作,核心功能包括:
- 图像识别定位:通过
pyautogui.locateOnScreen()定位屏幕元素(如按钮、输入框) - 模拟人机交互:使用
pyautogui.click()/doubleClick()模拟鼠标操作 - 数据自动化输入:结合
pyperclip.copy()与快捷键实现文本粘贴 - 异常处理机制:通过
try-except捕获图像识别失败等异常
该脚本展现了Python在GUI自动化领域的强大能力,其技术实现涉及多个底层机制。
3.核心功能实现原理
3.1 视觉定位引擎
采用pyautogui库构建的图像识别系统,通过模板匹配技术定位屏幕元素:
location = pyautogui.locateOnScreen('search_input.png', confidence=0.9)
该机制支持90%相似度的模糊匹配,可适应不同分辨率环境。
3.2人机交互模拟
结合鼠标键盘操作模拟,实现从搜索到提交的全流程自动化:
pyautogui.doubleClick() # 输入框激活
pyperclip.copy('深圳科技馆') # 文本复制
pyautogui.hotkey('ctrl', 'v') # 粘贴操作
3.3动态决策系统
通过滚动遍历与状态检测,智能判断活动可预约性:
if find_and_click('you_piao.png'): # 检测"有票"标识
fill_reservation_info() # 填写预约信息
4.技术架构亮点
- 跨平台兼容设计:采用绝对坐标+相对位移混合定位策略
- 异常容错机制:设置30秒超时重试与安全终止角(屏幕角落移动鼠标触发)
- 数据持久化:预约成功自动截屏存档
5.“深圳科学技术馆”自动预约功能源码
import pyautogui
import pyperclip
import time
import os
# 安全设置:在脚本运行前移动鼠标到角落可终止脚本
pyautogui.FAILSAFE = True
# 全局延迟设置,可根据需要调整
pyautogui.PAUSE = 1.5
def find_and_click(image_path, confidence=0.9, region=None):
"""查找并点击图片"""
try:
location = pyautogui.locateOnScreen(image_path, confidence=confidence, region=region)
if location:
center = pyautogui.center(location)
pyautogui.click(center)
return True
return False
except Exception as e:
print(f"查找图片 {image_path} 时出错: {e}")
return False
def find_and_click_and_input(image_path,text, confidence=0.9, region=None):
"""查找并点击图片"""
try:
location = pyautogui.locateOnScreen(image_path, confidence=confidence, region=region)
if location:
center = pyautogui.center(location)
pyautogui.moveTo(center.x, center.y, duration=0.5)
print(f"输入文字 {text} x={center.x} y={center.y}")
pyautogui.doubleClick() # 双击确保焦点
time.sleep(0.5)
# 输入文本
pyperclip.copy(text)
pyautogui.hotkey('ctrl', 'v')
pyautogui.press('enter')
print("输入文字完成")
return True
return False
except Exception as e:
print(f"查找图片 {image_path} 时出错: {e}")
return False
def wait_for_image(image_path, timeout=30, confidence=0.9, region=None):
"""等待图片出现"""
start_time = time.time()
while time.time() - start_time < timeout:
if find_and_click(image_path, confidence, region):
return True
time.sleep(0.5)
return False
def main():
print("脚本将在5秒后开始,请切换到微信窗口...")
time.sleep(5)
try:
# 第一步:搜索并打开深圳科学技术馆小程序
print("点击小程序图标...")
# 打开微信搜索框(假设微信已打开,这里需要根据实际情况调整)
# 可能需要先点击微信窗口或使用快捷键Ctrl+F
# 输入搜索内容(这里假设可以通过剪贴板粘贴)
if not find_and_click('F:\\2025_project\\pythonProject\\xiao_cheng_xu.png'): # 需要准备小程序图标截图
print("未找到小程序图标,尝试直接回车")
return
time.sleep(2)
print("点击小程序搜索按钮...")
if not find_and_click('F:\\2025_project\\pythonProject\\xiao_cheng_xu_input.png'): # 需要准备小程序图标截图
print("未找到小程序搜索输入框,尝试直接回车")
return
time.sleep(1)
print("搜索指定名称小程序...")
if not find_and_click_and_input('F:\\2025_project\\pythonProject\\search_input.png','深圳科技馆'): # 需要准备小程序图标截图
print("未找到小程序搜索输入框,尝试直接回车")
return
time.sleep(5)
# 点击搜索结果中的小程序
print("点击小程序...")
if not find_and_click('F:\\2025_project\\pythonProject\\shenz_kexue_jishu_logo.png'): # 需要准备小程序图标截图
print("未找到小程序图标,尝试直接回车")
return
# 等待小程序加载
time.sleep(5)
# 第二步:点击菜单 研学活动->科普活动
print("点击研学课程...")
# 点击"研学活动"菜单(需要准备截图)
if not wait_for_image('F:\\2025_project\\pythonProject\\yanxue.png'):
print("找不到研学活动菜单")
return
pyautogui.scroll(-1500)
time.sleep(2)
print("点击提示框我知道了...")
if not find_and_click('F:\\2025_project\\pythonProject\\i_know.png'):
print("找不到科普活动菜单")
return
time.sleep(1)
# 点击"科普活动"子菜单(需要准备截图)
print("点击科普活动...")
if not wait_for_image('F:\\2025_project\\pythonProject\\kepu_huodong.png'):
print("找不到科普活动子菜单")
return
# 等待活动列表加载
time.sleep(1)
# 第三步:遍历活动列表,查找可预约的活动
print("开始查找可预约的活动...")
activity_found = False
max_attempts = 5 # 最多尝试5个活动
for attempt in range(max_attempts):
if not find_and_click('F:\\2025_project\\pythonProject\\house.png'):
print("找不到house")
return
time.sleep(1)
value = -1000 * attempt
print(f"滚动条下滚{value}...")
pyautogui.scroll(value)
time.sleep(2)
print(f"尝试第 {attempt + 1} 个活动...")
# 可能需要向下滚动查找更多活动
# 查找"立即预约"按钮(需要准备截图)
if find_and_click('F:\\2025_project\\pythonProject\\lijiyuyue_button.png'):
time.sleep(1) # 等待预约页面加载
# 第四步:检查日期是否可预约
print("检查日期是否可预约,滚动条右滚...")
pyautogui.vscroll(200)
# 查找"不可预约"或"售罄"的标识(需要准备截图)
# 这里可能需要遍历多个日期,根据实际情况调整
all_sold_out = True
# 假设我们检查前3个日期
# for i in range(3):
# 查找可预约的日期(需要准备"可预约"状态的截图)
print("查找是否有票...")
if not find_and_click('F:\\2025_project\\pythonProject\\you_piao.png'):
all_sold_out = True
else:
all_sold_out = False
if all_sold_out:
print("当前活动不可预约,返回活动列表...")
# pyautogui.press('escape') # 假设按ESC返回
find_and_click('F:\\2025_project\\pythonProject\\back.png')
time.sleep(2)
else:
activity_found = True
break
if not activity_found:
print("没有找到可预约的活动")
return
# 第五步:填写预约信息并提交
print("找到可预约活动,开始填写信息...")
# 选择日期(如果尚未选择)
if not find_and_click('F:\\2025_project\\pythonProject\\date_available.png'):
print("无法选择日期")
return
# 填写预约信息(这里需要根据实际情况调整)
# 假设我们需要填写姓名、手机号等
# 点击姓名输入框(需要准备截图)
if find_and_click('F:\\2025_project\\pythonProject\\name_input.png'):
pyautogui.write('张三') # 替换为实际姓名
time.sleep(0.5)
# 点击手机号输入框(需要准备截图)
if find_and_click('F:\\2025_project\\pythonProject\\phone_input.png'):
pyautogui.write('13800138000') # 替换为实际手机号
time.sleep(0.5)
# 其他必填信息...
# 提交预约
print("提交预约...")
if find_and_click('F:\\2025_project\\pythonProject\\submit_button.png'):
time.sleep(3)
# 确认提交(如果有确认对话框)
if find_and_click('F:\\2025_project\\pythonProject\\confirm_button.png'):
print("预约提交成功!")
time.sleep(2)
else:
print("找不到提交按钮")
return
# 完成后的处理(如截图保存)
pyautogui.screenshot('预约成功.png')
print("预约流程完成")
except Exception as e:
print(f"发生错误: {e}")
finally:
print("脚本执行结束")
if __name__ == "__main__":
# 调整窗口大小,确保所有元素可见
print("请确保微信窗口已打开并显示在屏幕前")
main()

被折叠的 条评论
为什么被折叠?



