你是否遇到过这样的困扰?在答题类微信小程序中无法跳页,只能一页页翻看,一旦中途退出就要从头开始?本文将介绍一个高效解决方案,让技术小白也能轻松掌握自动截图技巧!
许多答题类微信小程序(如驾考题库、资格考试题库等)存在两大痛点:
通过技术手段实现:
这样就能在电脑上自由浏览所有截图,无需担心进度丢失!
ADB是连接电脑与Android设备的桥梁,实现命令控制。
为什么需要ADB?
它允许我们从电脑直接控制手机/模拟器执行操作,是自动化的基础。
操作步骤:
在电脑上模拟安卓环境运行微信小程序。
为什么需要模拟器?
直接在电脑上操作更便捷,且避免频繁操作真实手机。
操作步骤:
在模拟器中运行微信小程序。
操作步骤:
以下是完整的Python自动化脚本,包含详细注释:
import os
import subprocess
import time
import sys
import shutil
import imagehash
from PIL import Image
import cv2
import numpy as np
def run_exe_command(command):
"""执行adb.exe命令并返回输出"""
try:
result = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT)
return result.decode('utf-8').strip()
except subprocess.CalledProcessError as e:
print(f"命令执行失败: {e.output.decode('utf-8')}")
sys.exit(1)
def take_screenshot(filename):
"""截取屏幕并保存到本地"""
run_exe_command(f"adb.exe shell screencap /sdcard/screen.png")
run_exe_command(f"adb.exe pull /sdcard/screen.png")
shutil.move('screen.png',filename)
def get_screen_resolution():
"""获取设备屏幕分辨率"""
output = run_exe_command("adb.exe shell wm size")
resolution = output.split(": ")[-1]
width, height = map(int, resolution.split("x"))
return width, height
def swipe(start_x, start_y, end_x, end_y, duration_ms=300):
"""模拟滑动操作"""
run_exe_command(f"adb.exe shell input swipe {start_x} {start_y} {end_x} {end_y} {duration_ms}")
def cacl_distance(img1, img2):
diff = cv2.absdiff(img1, img2)
return diff.sum()
def main():
# 检查设备连接
devices = run_exe_command("adb.exe devices").splitlines()
if len(devices) < 2 or "device" not in devices[1]:
print("未找到连接的设备")
return
# 2. 获取屏幕分辨率
width, height = get_screen_resolution()
print(f"设备分辨率: {width}x{height}")
# 3. 计算滑动参数
center_y = height // 2
swipe_distance = width // 3 # 滑动距离为屏幕宽度的1/3
prefix="驾考题库截图"
os.makedirs(prefix, exist_ok=True)
seq=1
last_hash=None
while True:
# 截取屏幕
screenshot_name = os.path.join(prefix, f"{seq:04d}.png")
take_screenshot(screenshot_name)
# 计算hash
im1=np.array(Image.open(screenshot_name))
im1_gray = cv2.cvtColor(im1, cv2.COLOR_RGB2GRAY)
# 比较是否一致,如果没有更新就退出
if last_hash is not None:
distance = cacl_distance(im1_gray, last_hash)
print(screenshot_name,distance)
if distance<10000:
#os.remove(screenshot_name)
break
last_hash=im1_gray.copy()
# 模拟向左滑动(从右到左)
swipe(
start_x=width - swipe_distance // 2,start_y=center_y,
end_x=swipe_distance // 2,end_y=center_y,duration_ms=200)
time.sleep(2) # 等待操作完成
seq+=1
if __name__ == "__main__":
main()
智能停止机制
if diff < 10000:
break
滑动参数计算
center_y = height // 2
swipe_distance = width // 3
文件命名规范
filename = os.path.join(folder_name, f"{seq:04d}.png")
成功运行后,你将获得:
按顺序命名的截图文件
/驾考题库截图
├── 0001.png
├── 0002.png
├── ...
└── 0105.png
控制台实时输出
设备分辨率: 720x1280
截图 0001.png | 差异值: 0
截图 0002.png | 差异值: 38452
截图 0003.png | 差异值: 40128
...
截图 0087.png | 差异值: 9832
生成PDF文档
convert *.png 驾考题库.pdf
使用ImageMagick将截图合并为PDF文档
添加OCR文字识别
使用PaddleOCR等工具提取截图中的题目文本
自动答题功能
结合图像识别和ADB点击操作实现自动答题
通过本文介绍的方法,你可以轻松实现微信小程序的自动截图和翻页。这种方法特别适用于:
技术不是目的,而是解放生产力的工具。掌握这个自动化技巧,每次可节省数小时的手动操作时间!如果你有任何问题或改进建议,欢迎在评论区交流讨论。
提示:请合法使用自动化工具,遵守微信小程序的使用条款