此脚本仅为个人学习编程使用,无意实现作弊目的!!!

使用本脚本产生的一切后果自负!!!

这是我第一次写python脚本,肯定有许多不足之处,这篇文章也是记录一下我学习的过程。大佬轻喷

脚本的大致思路是用PaddleOCR识别后再用deepseek给出答案,接着用pyautogui实现自动点击。准确率在95%以上,通过修改提词可以提高答题准确率。但是我懒得试

注意此脚本OCR识别有不明原因报错,可能导致答题终止。

首先,确保你已经安装了相应库。如果还没有安装,可以使用以下命令安装:

pip install openai
pip install paddlepaddle
pip install paddleocr
pip install pyautogui

脚本代码:

import pyautogui
import time
from paddleocr import PaddleOCR
from openai import OpenAI
from PIL import Image

question_left = 74
question_top = 236
width = 500
height = 70
choice_left = 62
click_x = 1224

upper_bound = (222, 222, 222)
lower_bound = (114, 114, 114)

ocr = PaddleOCR(use_angle_cls=True, lang='ch')

def extract_text(image_path):
    result = ocr.ocr(image_path, cls=True)
    if not result:  # 如果结果是None或者空
        print(f"OCR失败,图片路径:{image_path}")  # 打印出OCR失败的提示,帮助调试
        return ""
    print(result)  # 调试输出,查看OCR的返回结构
    text = "".join([line[1][0] for line in result[0]])
    return text

def answer_question_loop():
    for _ in range(100):
        screenshot_q = pyautogui.screenshot(region=(question_left, question_top, width, height))
        screenshot_a = pyautogui.screenshot(region=(choice_left, 320, width, height))
        screenshot_b = pyautogui.screenshot(region=(choice_left, 400, width, height))
        screenshot_c = pyautogui.screenshot(region=(choice_left, 480, width, height))
        screenshot_d = pyautogui.screenshot(region=(choice_left, 560, width, height))
        screenshot_q.save("screenshot_question.png")
        screenshot_a.save("screenshot_a.png")
        screenshot_b.save("screenshot_b.png")
        screenshot_c.save("screenshot_c.png")
        screenshot_d.save("screenshot_d.png")
        image = Image.open("screenshot_question.png")
        pixels = image.load()
        # 遍历图片中的每个像素
        for y in range(image.height):
            for x in range(image.width):
                r, g, b = pixels[x, y]
                # 检查是否在指定范围内
                if lower_bound <= (r, g, b) <= upper_bound:
                    # 将该像素设置为白色
                    pixels[x, y] = (255, 255, 255)
        # 保存修改后的图片
        image.save("screenshot_question.png")
        text_q = extract_text("screenshot_question.png")
        text_a = extract_text("screenshot_a.png")
        text_b = extract_text("screenshot_b.png")
        text_c = extract_text("screenshot_c.png")
        text_d = extract_text("screenshot_d.png")
        print(f"\n{text_q}\n{text_a}\n{text_b}\n{text_c}\n{text_d}")
        con = f"""
        question:{text_q}\nA:{text_a}\nB:{text_b}\nC:{text_c}\nD:{text_d}
        """
        client = OpenAI(api_key="<DeepSeek API Key>", base_url="https://api.deepseek.com")
        response = client.chat.completions.create(
            model="deepseek-chat",
            messages=[
                {"role": "system",
                 "content": 'user提供一个question和4个选项,在4个选项中找出与question相同意思的答案!若无意思相同的答案,则找出意思最相近的,最终回答直接给出选项的大写字母,即ABCD中的一个字母'},
                {"role": "user", "content": con},
            ],
            stream=False
        )
        answer = response.choices[0].message.content
        print(f"回答: {answer}")
        if answer == 'A':
            pyautogui.moveTo(click_x, 340, duration=0.1)
            pyautogui.click()
        elif answer == 'B':
            pyautogui.moveTo(click_x, 425, duration=0.1)
            pyautogui.click()
        elif answer == 'C':
            pyautogui.moveTo(click_x, 510, duration=0.1)
            pyautogui.click()
        elif answer == 'D':
            pyautogui.moveTo(click_x, 590, duration=0.1)
            pyautogui.click()
        time.sleep(0.5)

# 开始回答问题循环
answer_question_loop()

#交卷
pyautogui.moveTo(1220, 145, duration=0.1)
pyautogui.click()
time.sleep(0.5)
pyautogui.moveTo(750, 757, duration=0.1)
pyautogui.click()

使用方法:

在edge浏览器内安装User-Agent Switcher and Manager扩展。使用前将浏览器UA设置为安卓。

进入https://www.deepseek.com/注册一个账户,将你的api key填入api_key="<DeepSeek API Key>"中,每月有500万的免费token,对于我爱记单词答题来说足够了。

接着打开https://skl.hduhelp.com/,用windows自带的分屏功能将我爱记单词放在左边,脚本放在右边,各占一半,如果使用的是分辨率为2560*1440(2K)的屏幕+edge浏览器的话,一般可以直接使用我的参数,如果不是,请自行修改screenshot与pyautogui.moveTo中的像素坐标,可以截全屏后用使用ps查看各像素的坐标

做好这些工作之后点击开始考试,然后运行脚本即可。

注意:该脚本不会自动提交试卷,请在答题结束后手动提交!!!

更新版本后添加了自动提交功能和过滤图像无效信息的功能。