python脚本—3.批量导出学习通作业

发布时间:2026/7/11 10:51:37

python脚本—3.批量导出学习通作业 任务背景我需要在学习通上4次作业一共5个班级每个班级每个作业需要导出5名同学的作业附件一共需要导出200个作业附件。如果自己手动操作太麻烦了就写了这个python脚本。1.首先问了QODER CN但是涉及到登录问题它写的代码时需要在登录页面扫码登录或手动输入账号密码很麻烦基于安全我没有告诉它账号密码。QODER CN 对话这个程序更多是自己手写的涉及到网页HTML的解析所有自己一点一点的分析出来的。代码主要用到selenium库。‌Selenium库是一个用于 Web 应用程序自动化测试的开源工具‌也能用来做爬虫支持 Python、Java 等多种语言可以模拟真实用户操作浏览器 。2.代码如下需要修改45行账号和48行密码。把代码中*****改为自己的账号和密码。第21行为作业的页面的url。不需要修改。代码的基本逻辑1.(login_xuexit)登录到学习通进入作业界面2.(select_hw)找到对应的作业3.(download_hw)点击批阅选择5个同学点击“批量导出作业附件”4.(next_page)在学习通作业页面每页显示12个作业项需要点击下一页继续查找作业。 Autor: Liu Wenying Date: 2026-07-06 10:04:31 Description: LastEditors: Please set LastEditors LastEditTime: 2026-07-10 12:13:24 from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import time from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.common.exceptions import StaleElementReferenceException class DownloadHW: def __init__(self): self.url https://mooc2-ans.chaoxing.com/mooc2-ans/work/list?courseid260949743clazzid142233820courseId260949743classId142233820clazzId142233820cpi346300862enc196d8d881c164f5a313cbdb86c63ab5bopenc5adb563f7d4d7c0d6b7c30806fb32117t1783303178397utt classmethod def setup_class(self): # Set up Chrome options options webdriver.ChromeOptions() prefs { download.default_directory: /path/to/download/folder, plugins.always_open_pdf_externally: True } options.add_experimental_option(prefs, prefs) self.driver webdriver.Chrome(optionsoptions) def login_xuexit(self): try: # 1. Navigate to the page (Generic URL) self.driver.get(self.url) # Wait for the page to load completely wait WebDriverWait(self.driver, 10) # 输入账号和密码 phone_box self.driver.find_element(By.ID, phone) phone_box.send_keys(****) pwd_box self.driver.find_element(By.ID, pwd) pwd_box.send_keys(*****) # 点击登录 login_button wait.until( EC.element_to_be_clickable((By.ID, loginBtn))) login_button.click() print(登录成功) time.sleep(2) except Exception as e: print(fAn error occurred: {e}) # finally: # self.driver.quit() def next_page(self): 翻页 current_page self.driver.find_element(By.CLASS_NAME, xl-active) current_page int(current_page.text) print(当前页:, current_page) # if current_page ! 1: try: last_page self.driver.find_element(By.CLASS_NAME, xl-nextPage) is_last_page xl-disabled in last_page.get_attribute(class) print(是否可以点击下一页:, is_last_page) if not is_last_page: self.driver.find_element(By.CLASS_NAME, xl-nextPage).click() print(f已切换到第 {int(current_page)1} 页) time.sleep(2) self.select_hw(self.hw_name) else: print(已到达最后一页已经全部下载完) # self.select_hw(self.hw_name) self.driver.quit() return except Exception: self.driver.quit() def select_hw(self, hw_name): print(开始选择作业:, hw_name) self.hw_name hw_name max_retries 1 for attempt in range(max_retries): try: # 使用显式等待确保元素加载完成 WebDriverWait(self.driver, 10).until( EC.presence_of_all_elements_located( (By.XPATH, //div[classtaskList]/ul/li))) find_hw self.driver.find_elements( By.XPATH, //div[classtaskList]/ul/li) for i in range(len(find_hw)): try: # 每次点击前重新获取当前元素 current_list self.driver.find_elements( By.XPATH, //div[classtaskList]/ul/li) parent current_list[i] if hw_name in parent.text: print(当前作业:, hw_name, parent.text) self.download_hw(parent) except StaleElementReferenceException: print(f元素失效重新尝试第 {i1} 个...) break # 跳出内层循环重新开始外层循环 except Exception as e: print(f尝试 {attempt1} 失败{e}) if attempt max_retries - 1: raise e # finally: # self.driver.quit() self.next_page() def download_hw(self, parent): 下载作业 print(开始下载作业) try: review_button parent.find_elements( By.CLASS_NAME, wid9) # 假设批阅按钮的ID是review_button # print(len(review_button)) review_button[0].click() time.sleep(3) check_box self.driver.find_elements(By.CLASS_NAME, dataBody_check) # 2. 切片获取前5个元素并遍历点击 try: for element in check_box[:5]: element.click() time.sleep(1) # 如果点击后页面跳转或刷新可能需要重新定位元素或添加等待 # break 如果每次点击都导致页面变化通常不能连续点击需根据业务逻辑调整 except Exception as e: print(f点击失败: {e}) time.sleep(3) # 点击导出作业 export_button self.driver.find_element(By.CLASS_NAME, exportWorkAttachment) export_button.click() time.sleep(1) # 点击PDF格式 format_pdf_button self.driver.find_element( By.XPATH, //span[classformat_check and data1]) format_pdf_button.click() time.sleep(2) # 点击确认 confirm_button self.driver.find_element(By.CLASS_NAME, confirmDown) confirm_button.click() print(点击确认下载) time.sleep(2) # 点击返回 return_a self.driver.find_element(By.CLASS_NAME, icon-backIc) return_a.click() time.sleep(2) except Exception as e: print(fAn error occurred: {e}) if __name__ __main__: hw_name [第一章作业, 第二章作业] download_hw DownloadHW() download_hw.setup_class() download_hw.login_xuexit() for cur_hw in hw_name: download_hw.select_hw(cur_hw)完成作业附件导出。后续需要自己在“下载中心”手动下载。

相关新闻