from selenium.webdriver.chrome.options import Options
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
import os
import random


def get_driver():
    # 检查是否已经打开浏览器
    browser_opened = False
    for handle in webdriver.Chrome().window_handles:
        browser_opened = True
        break

    # 创建浏览器实例或在已有浏览器中操作
    if browser_opened:
        options = Options()
        options.debugger_address = "127.0.0.1:9222"
        driver = webdriver.Chrome(options=options)
    else:
        os.system(r'start chrome --remote-debugging-port=9222 --user-data-dir="D:\评阅用"')
        options = Options()
        options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
        driver = webdriver.Chrome(options=options)

    return driver


driver = get_driver()

# 找到有'内蒙古开放大学'字样的标签页
while True:
    for handle in driver.window_handles:
        driver.switch_to.window(handle)
        if '内蒙古开放大学' in driver.title:
            print("登录成功")
            break
    else:
        time.sleep(3)
        continue
    break

# 遍历题目组,查找待批改和题号元素
elements = driver.find_elements(By.CLASS_NAME, "subject-table-row")
for element in elements:
    # 查找待批改元素
    card_score_element = element.find_element(By.CLASS_NAME, "card-score")
    if '待批改' not in card_score_element.text:
        continue
    # 查找题号元素
    subject_number_element = element.find_element(By.CLASS_NAME, "subject-number")
    subject_number_text = subject_number_element.text
    # 提取题号
    subject_number = ""
    for char in subject_number_text:
        if char.isdigit():
            subject_number += char
    print("待批改的题目号:", subject_number)

    # 查找答案和分数
    summary_sub_title_elements = driver.find_elements(By.CLASS_NAME, "summary-sub-title")
    answer_content_elements = driver.find_elements(By.CLASS_NAME, "answer-content")
    for i in range(len(summary_sub_title_elements)):
        summary_sub_title_element = summary_sub_title_elements[i]
        answer_content_element = answer_content_elements[i]
        if '第' + subject_number + '题' in summary_sub_title_element.text and '得分' in answer_content_element.text:
            score = ""
            for char in card_score_element.text:
                if char.isdigit():
                    score += char
            print("分数:", score)
            print("答案:", answer_content_element.text)
            break
    else:
        print("未找到该题目的答案和分数")
    
driver.quit()

该脚本使用 Python Selenium 库实现以下功能:

  • 自动打开内蒙古开放大学的作业平台并登录
  • 查找所有待批改的题目
  • 提取每个待批改题目的题号
  • 查找每个待批改题目的答案和分数
  • 打印每个待批改题目的题号、分数和答案

使用方法:

  1. 确保已安装 Python 和 Selenium 库。
  2. 修改脚本中的 --user-data-dir 参数为你的 Chrome 用户数据目录,该目录存储了你的登录信息等数据。
  3. 运行脚本。

注意:

  • 该脚本仅供学习和研究使用,请勿用于任何非法目的。
  • 该脚本可能需要根据网站结构进行调整。
  • 使用该脚本可能存在风险,请谨慎使用。

建议:

  • 使用更稳定的 WebDriver API,如 WebDriverWaitExpectedConditions,来提高脚本的稳定性。
  • 将脚本中的硬编码数据,例如网站地址、用户名和密码等,替换为变量或配置文件,以提高脚本的可维护性。
  • 使用日志记录功能,记录脚本执行过程中的信息,方便调试和排查问题。

希望该脚本能帮助你完成内蒙古开放大学作业的批改工作。如果你有任何问题,请随时提出。


原文地址: https://www.cveoy.top/t/topic/fZZH 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录