• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

pyautogui微信小程序自动化操作

武飞扬头像
金汞门
帮助1

一个很简单的脚本,用途是实现微信小程序或其他软件的自动化操作,如果是长期固定需求,可以考虑把脚本内置到常用软件的环境中,比如MAYA,当我每天打开MAYA时就可以实现每天自动在小程序上疫情打卡

主要使用pyautogui和opencv模块,opencv用于图像识别,pyautogui用于键鼠操作

  1.  
    import pyautogui as pgi
  2.  
    import time
  3.  
    import cv2

此处设置为相对路径,运行前提前将小程序图标截图放在这个路径下命名为daka.jpg

  1.  
    # 寻找桌面图标:
  2.  
    # 用opencv识别桌面图像,并与路径中的图像比对,输出中心点。
  3.  
    # pyautogui.locateonscreem也可以识别屏幕,但是识别对分辨率要求较高,差一个像素点都会导致错误,返回none,采用opencv直接识别,大部分情况都会返回一个结果(不一定是目标结果)
  4.  
    def findProc(img_path):
  5.  
    pgi.screenshot().save(r'.\img\screenshot.png')
  6.  
    img = cv2.imread(r'.\img\screenshot.png')
  7.  
    img_terminal = cv2.imread(r'.\img\daka.jpg')
  8.  
    height = img_terminal.shape[0]
  9.  
    width = img_terminal.shape[1]
  10.  
    result = cv2.matchTemplate(img, img_terminal, cv2.TM_SQDIFF_NORMED)
  11.  
    upper_left = cv2.minMaxLoc(result)[2]
  12.  
    center = (int(upper_left[0] width/2), int(upper_left[1] height/2))
  13.  
    return center

主程序部分是针对同济大学疫情上报小程序写的,用于实现自动填写信息并完成上报和关闭窗口

  1.  
    # 主程序:
  2.  
    def autoClick(var_center):
  3.  
    pgi.doubleClick(var_center[0], var_center[1])
  4.  
    time.sleep(2)
  5.  
    pgi.click(1150, 690)# 进入界面
  6.  
    time.sleep(1)
  7.  
    pgi.click(1350, 730)# 健康状态
  8.  
    time.sleep(0.2)
  9.  
    pgi.click(1510, 730)# 确定
  10.  
    time.sleep(0.1)
  11.  
    pgi.click(1336, 807)# 隔离状态
  12.  
    time.sleep(0.2)
  13.  
    pgi.click(1500, 724)# 确定
  14.  
    time.sleep(0.1)
  15.  
    pgi.click(1330, 863)# 今日体温
  16.  
    time.sleep(0.01)
  17.  
    pgi.press(['3', '6'])
  18.  
    pgi.click(1280, 1046)# 上报
  19.  
    time.sleep(0.1)
  20.  
    pgi.click(1504, 234)# 关闭
学新通
  1.  
    # 执行:
  2.  
    # 执行主程序并输出日志文件,记录本次操作的日期时间
  3.  
    def routine(img_path, name):
  4.  
    center = findProc(img_path)
  5.  
    autoClick(center)
  6.  
    output_path = r'.\img\log.txt'
  7.  
    now = time.localtime()
  8.  
    now_time = time.strftime("%Y-%m-%d %H:%M:%S", now)
  9.  
    with open(output_path, 'w ', encoding='utf-8') as file1:
  10.  
    print(now_time, file=file1)
  1.  
    # 判断上次更新时间
  2.  
    # 获取日志文件中的信息,查看今天是否已经执行过程序,如果没有执行过,则会执行
  3.  
    def judgeLastTime():
  4.  
    with open(r'.\img\log.txt', 'r ', encoding='utf-8') as file1:
  5.  
    lastDate = ''
  6.  
    nowDate = ''
  7.  
    data1 = file1.readlines()
  8.  
    if data1 == []:
  9.  
    routine(r'.\img\screenshot.png', 'daka')
  10.  
    else:
  11.  
    lastDate = data1[0].split( )[0]
  12.  
    now = time.localtime()
  13.  
    nowDate = str(time.strftime("%Y-%m-%d %H:%M:%S", now)).split( )[0]
  14.  
    if nowDate != lastDate:
  15.  
    routine(r'.\img\screenshot.png', 'daka')
学新通

运行程序 

judgeLastTime()

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgahffh
系列文章
更多 icon
同类精品
更多 icon
继续加载