基本完成小闲平台答题zip整屏自动下载并记录中位提交时间

This commit is contained in:
weiye.wang 2024-03-09 20:19:58 +08:00
parent 90094259e1
commit 7b32e1ba4f
3 changed files with 534 additions and 0 deletions

View File

@ -0,0 +1,91 @@
# -*- coding: utf-8 -*-
################################################################################
## Form generated from reading UI file '下载小闲答题情况.ui'
##
## Created by: Qt User Interface Compiler version 6.6.2
##
## WARNING! All changes made in this file will be lost when recompiling UI file!
################################################################################
from PySide6.QtCore import (QCoreApplication, QDate, QDateTime, QLocale,
QMetaObject, QObject, QPoint, QRect,
QSize, QTime, QUrl, Qt)
from PySide6.QtGui import (QBrush, QColor, QConicalGradient, QCursor,
QFont, QFontDatabase, QGradient, QIcon,
QImage, QKeySequence, QLinearGradient, QPainter,
QPalette, QPixmap, QRadialGradient, QTransform)
from PySide6.QtWidgets import (QApplication, QLabel, QLineEdit, QPushButton,
QSizePolicy, QWidget)
class Ui_Form(object):
def setupUi(self, Form):
if not Form.objectName():
Form.setObjectName(u"Form")
Form.resize(492, 192)
self.pushButton_chrome = QPushButton(Form)
self.pushButton_chrome.setObjectName(u"pushButton_chrome")
self.pushButton_chrome.setGeometry(QRect(10, 10, 75, 24))
self.pushButton_driver = QPushButton(Form)
self.pushButton_driver.setObjectName(u"pushButton_driver")
self.pushButton_driver.setGeometry(QRect(10, 50, 75, 24))
self.pushButton_outputfolder = QPushButton(Form)
self.pushButton_outputfolder.setObjectName(u"pushButton_outputfolder")
self.pushButton_outputfolder.setGeometry(QRect(10, 90, 75, 24))
self.label_chrome = QLabel(Form)
self.label_chrome.setObjectName(u"label_chrome")
self.label_chrome.setGeometry(QRect(90, 10, 391, 21))
self.label_driver = QLabel(Form)
self.label_driver.setObjectName(u"label_driver")
self.label_driver.setGeometry(QRect(90, 50, 391, 21))
self.label_outputfolder = QLabel(Form)
self.label_outputfolder.setObjectName(u"label_outputfolder")
self.label_outputfolder.setGeometry(QRect(90, 90, 391, 21))
self.pushButton_openbrowser = QPushButton(Form)
self.pushButton_openbrowser.setObjectName(u"pushButton_openbrowser")
self.pushButton_openbrowser.setGeometry(QRect(10, 130, 71, 51))
self.pushButton_login = QPushButton(Form)
self.pushButton_login.setObjectName(u"pushButton_login")
self.pushButton_login.setGeometry(QRect(90, 130, 71, 51))
self.pushButton_exec = QPushButton(Form)
self.pushButton_exec.setObjectName(u"pushButton_exec")
self.pushButton_exec.setGeometry(QRect(340, 130, 141, 51))
font = QFont()
font.setBold(True)
self.pushButton_exec.setFont(font)
self.label_4 = QLabel(Form)
self.label_4.setObjectName(u"label_4")
self.label_4.setGeometry(QRect(170, 130, 91, 51))
self.label_4.setAlignment(Qt.AlignCenter)
self.label_5 = QLabel(Form)
self.label_5.setObjectName(u"label_5")
self.label_5.setGeometry(QRect(270, 140, 54, 16))
self.lineEdit_num = QLineEdit(Form)
self.lineEdit_num.setObjectName(u"lineEdit_num")
self.lineEdit_num.setGeometry(QRect(260, 160, 71, 20))
self.retranslateUi(Form)
QMetaObject.connectSlotsByName(Form)
# setupUi
def retranslateUi(self, Form):
Form.setWindowTitle(QCoreApplication.translate("Form", u"\u4e0b\u8f7d\u5c0f\u95f2\u7b54\u9898\u7eb8", None))
self.pushButton_chrome.setText(QCoreApplication.translate("Form", u"chrome\u8def\u5f84", None))
self.pushButton_driver.setText(QCoreApplication.translate("Form", u"driver\u8def\u5f84", None))
self.pushButton_outputfolder.setText(QCoreApplication.translate("Form", u"\u8f93\u51fa\u8def\u5f84", None))
self.label_chrome.setText(QCoreApplication.translate("Form", u"\u6b64\u5904\u663e\u793achrome.exe\u7684\u8def\u5f84", None))
self.label_driver.setText(QCoreApplication.translate("Form", u"\u6b64\u5904\u663e\u793achrome webdriver\u7684\u8def\u5f84", None))
self.label_outputfolder.setText(QCoreApplication.translate("Form", u"\u6b64\u5904\u663e\u793a\u8f93\u51fa\u7684zip\u6587\u4ef6\u7684\u8def\u5f84", None))
self.pushButton_openbrowser.setText(QCoreApplication.translate("Form", u"\u5f00\u542f\u6d4f\u89c8\u5668", None))
self.pushButton_login.setText(QCoreApplication.translate("Form", u"\u767b\u5f55", None))
self.pushButton_exec.setText(QCoreApplication.translate("Form", u"\u4e0b\u8f7d\u7b54\u9898\u60c5\u51b5", None))
self.label_4.setText(QCoreApplication.translate("Form", u"\u5728\u5c0f\u95f2\u9875\u9762\u7684\n"
"\u5b66\u60c5\u62a5\u544a\u4e2d\n"
"\u7ffb\u5230\u6307\u5b9a\u9875\u9762", None))
self.label_5.setText(QCoreApplication.translate("Form", u"\u4e0b\u8f7d\u7f16\u53f7", None))
self.lineEdit_num.setInputMask("")
self.lineEdit_num.setText("")
self.lineEdit_num.setPlaceholderText(QCoreApplication.translate("Form", u"\u7559\u7a7a\u4e3a\u5168\u90e8", None))
# retranslateUi

View File

@ -0,0 +1,252 @@
from PySide6.QtWidgets import QWidget, QApplication, QFileDialog
from Ui_下载小闲答题情况 import Ui_Form
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.keys import Keys
import re,os
from datetime import datetime
import numpy as np
from time import sleep
def GetPageInfo(driver): #学情报告页面, 显示页码和总页数
pageinfo = driver.find_element(By.CSS_SELECTOR,"[class*=styles__page___]").text
return pageinfo
def SelectNext(driver): # 在打开后的报告页面选择下一个班级
try:
openlist = driver.find_element(By.XPATH,"/html/body/div[2]/div/div/div[2]/div[1]/div[2]/div/div/div")
sleep(0.1)
openlist.click()
sleep(0.1)
openlist.send_keys(Keys.ARROW_DOWN)
sleep(0.1)
openlist.send_keys(Keys.ENTER)
sleep(0.3)
return driver.find_element(By.XPATH,"/html/body/div[2]/div/div/div[2]/div[1]/div[2]/div/div/div").text
except:
return 1
def GetDate(driver): #显示当前班级的提交时间
commit_date = driver.find_element(By.XPATH,"/html/body/div[2]/div/div/div[2]/div[1]/div[2]/span[2]").text
year,month,date = re.findall(r"(\d{4})-(\d{2})-(\d{2})",commit_date)[0]
return year+month+date
def GetCommitInfo(driver): #获取报告页面的班级及提交日期信息
commit_dict = {}
classname = SelectNext(driver)
sleep(0.5)
while not classname in commit_dict.keys():
commit_date = GetDate(driver)
if not classname == 1:
commit_dict[classname] = commit_date
classname = SelectNext(driver)
else:
classname = SelectNext(driver)
return commit_dict
def ToIntTime(string):
date_obj = datetime.strptime(string, "%Y%m%d")
int_time = int(date_obj.timestamp())
return int_time
def MedianCommitDate(adict):
return datetime.fromtimestamp(np.median([ToIntTime(adict[t]) for t in adict.keys()])).strftime("%Y%m%d")
def gotopagenum(driver,num):
pagecontrol = driver.find_element(By.CSS_SELECTOR,"[class*=styles__pagination___]")
pageinput = pagecontrol.find_element(By.CSS_SELECTOR,"input[type=text]")
pageinput.clear()
submitbutton = pagecontrol.find_element(By.CSS_SELECTOR,"[class*=tyles__button___]")
pageinput.send_keys(str(1))
submitbutton.click()
sleep(0.5)
pageinput.clear()
sleep(0.1)
pageinput.send_keys(str(num))
submitbutton.click()
sleep(0.5)
def ClickBack(driver): # 点击返回按钮
backbutton = driver.find_element(By.CSS_SELECTOR,'[class*=styles__back]') # 点击返回
backbutton.click()
sleep(1)
def GetHomeworkNames(driver,starting_date = "20000101"):
hwk_dict = {}
homework_roots = driver.find_elements(By.CSS_SELECTOR,'[class*=styles__reportList]')
for homework_root in homework_roots:
name = homework_root.find_element(By.CSS_SELECTOR,'[class*=styles__name]').text
date = homework_root.find_element(By.CSS_SELECTOR,'[class*=styles__time]').text
date = re.findall(r"\d{4}-\d{2}-\d{2}",date)[0].replace("-","")
grade = homework_root.find_element(By.CSS_SELECTOR,'[class*=styles__grades]').text[3:]
if date >= starting_date:
hwk_dict[name] = {"name": name, "date": date, "grade": grade}
return hwk_dict
def GetHomeworkRoots(driver): #找到页面上所有作业考试的root
homework_roots = driver.find_elements(By.CSS_SELECTOR,'[class*=styles__reportList]')
return homework_roots
def ClickDetail(root): #点击查看详情
root.find_element(By.XPATH,"div[2]/div[2]").click()
def ClickDownload(root): #点击报表
root.find_element(By.XPATH,"div[2]/div[3]").click()
def StartandEndDownload(driver):
driver.find_element(By.XPATH,"/html/body/div[4]/div/div[2]/div/div[2]/div[3]/div/button[2]").click()
QuitDownload(driver)
def QuitDownload(driver): #退出下载对话框
while driver.find_element(By.XPATH,"/html/body/div[4]/div/div[2]/div/div[2]/div[3]/div/button[1]").text == '取 消':
try:
sleep(0.5)
driver.find_element(By.XPATH,"/html/body/div[4]/div/div[2]/div/div[2]/div[3]/div/button[1]").click()
except:
pass
def GenerateCommitMessages(driver,alist = []): #在班级报告页面逐一读取页面上的指定作业的名称和计算中位提交日期
messages = {}
page_info = GetPageInfo(driver)
page = page_info[:page_info.index("/")]
roots_len = len(GetHomeworkRoots(driver))
if alist == []:
rangelist = range(roots_len)
else:
rangelist = alist.copy()
for i in rangelist:
roots = GetHomeworkRoots(driver)
root = roots[i]
root_text = root.text
homework_name = root_text.split("\n")[1]
ClickDetail(root)
adict = GetCommitInfo(driver)
print(homework_name)
messages[homework_name] = MedianCommitDate(adict)
sleep(0.1)
ClickBack(driver)
sleep(0.5)
gotopagenum(driver,page)
sleep(0.5)
return messages
def DownloadZips(driver,alist=[]): #下载页面上的指定zip文件
page_info = GetPageInfo(driver)
page = page_info[:page_info.index("/")]
roots_len = len(GetHomeworkRoots(driver))
if alist == []:
rangelist = range(roots_len)
else:
rangelist = alist.copy()
for i in rangelist:
roots = GetHomeworkRoots(driver)
root = roots[i]
root_text = root.text
homework_name = root_text.split("\n")[1]
ClickDownload(root)
sleep(0.5)
StartandEndDownload(driver)
print(f"已下载: {homework_name}")
sleep(0.5)
gotopagenum(driver,page)
sleep(0.5)
def DownloadandRenameZips(driver,commit_info,folder,alist=[]): #下载页面上的指定zip文件
page_info = GetPageInfo(driver)
page = page_info[:page_info.index("/")]
roots_len = len(GetHomeworkRoots(driver))
if alist == []:
rangelist = range(roots_len)
else:
rangelist = alist.copy()
for i in rangelist:
roots = GetHomeworkRoots(driver)
root = roots[i]
root_text = root.text
homework_name = root_text.split("\n")[1]
ClickDownload(root)
sleep(0.5)
StartandEndDownload(driver)
print(f"已下载: {homework_name}")
date = commit_info[homework_name]
sleep(5)
filelist = os.listdir(folder)
filelist.sort(key = lambda x:os.path.getmtime(os.path.join(folder,x)),reverse = True)
filepath = os.path.join(folder,filelist[0])
renamedfilepath = filepath[:-4]+f"({date}).zip"
os.rename(filepath,renamedfilepath)
print(f"已重命名为: {os.path.split(renamedfilepath)[-1]}")
sleep(0.5)
gotopagenum(driver,page)
sleep(0.5)
def getIndices(string):
if string.strip() == "":
return []
else:
string_list = [int(i)-1 for i in string.strip().split(",")]
return string_list
class MyWindow(QWidget,Ui_Form):
def __init__(self):
super().__init__()
self.setupUi(self)
self.bind()
def bind(self):
self.pushButton_chrome.clicked.connect(self.getchromePath)
self.pushButton_driver.clicked.connect(self.getdriverPath)
self.pushButton_outputfolder.clicked.connect(self.getoutputfolder)
self.pushButton_openbrowser.clicked.connect(self.openbrowser)
self.pushButton_login.clicked.connect(self.login)
self.pushButton_exec.clicked.connect(self.exec)
def getchromePath(self):
pathlist = QFileDialog.getOpenFileName(self,"选择文件",".","chrome.exe文件(chrome.exe);;所有文件(*)")
self.label_chrome.setText(pathlist[0])
self.chromepath = pathlist[0]
def getdriverPath(self):
pathlist = QFileDialog.getOpenFileName(self,"选择文件",".","chromedriver.exe文件(chromedriver.exe);;所有文件(*)")
self.label_driver.setText(pathlist[0])
self.driverpath = pathlist[0]
def getoutputfolder(self):
self.outputfolder = QFileDialog.getExistingDirectory(None, "选择文件夹").replace("/","\\")
self.label_outputfolder.setText(self.outputfolder)
def openbrowser(self):
service = Service(executable_path=self.driverpath)
options = webdriver.ChromeOptions()
options.binary_location = self.chromepath
prefs = {"download.default_directory": self.outputfolder}
print(self.outputfolder)
options.add_experimental_option("prefs", prefs)
self.driver = webdriver.Chrome(service= service,options = options)
self.driver.get("http://ls.xiaoxianai.cn")
def login(self):
loginform = self.driver.find_element(By.XPATH,'//*[@id="container"]/div/div/form')
loginlist = loginform.find_elements(By.TAG_NAME,"div")
loginlist[1].find_element(By.TAG_NAME,"input").send_keys("16621337584")
loginlist[2].find_element(By.TAG_NAME,"input").send_keys("password")
loginform.find_element(By.TAG_NAME,"button").click()
def exec(self):
commit_dict = GenerateCommitMessages(self.driver,getIndices(self.lineEdit_num.text()))
DownloadandRenameZips(self.driver,commit_dict,self.outputfolder,getIndices(self.lineEdit_num.text()))
if __name__ == '__main__':
app = QApplication([])
windows = MyWindow()
windows.show()
app.exec()

View File

@ -0,0 +1,191 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form</class>
<widget class="QWidget" name="Form">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>492</width>
<height>192</height>
</rect>
</property>
<property name="windowTitle">
<string>下载小闲答题纸</string>
</property>
<widget class="QPushButton" name="pushButton_chrome">
<property name="geometry">
<rect>
<x>10</x>
<y>10</y>
<width>75</width>
<height>24</height>
</rect>
</property>
<property name="text">
<string>chrome路径</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_driver">
<property name="geometry">
<rect>
<x>10</x>
<y>50</y>
<width>75</width>
<height>24</height>
</rect>
</property>
<property name="text">
<string>driver路径</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_outputfolder">
<property name="geometry">
<rect>
<x>10</x>
<y>90</y>
<width>75</width>
<height>24</height>
</rect>
</property>
<property name="text">
<string>输出路径</string>
</property>
</widget>
<widget class="QLabel" name="label_chrome">
<property name="geometry">
<rect>
<x>90</x>
<y>10</y>
<width>391</width>
<height>21</height>
</rect>
</property>
<property name="text">
<string>此处显示chrome.exe的路径</string>
</property>
</widget>
<widget class="QLabel" name="label_driver">
<property name="geometry">
<rect>
<x>90</x>
<y>50</y>
<width>391</width>
<height>21</height>
</rect>
</property>
<property name="text">
<string>此处显示chrome webdriver的路径</string>
</property>
</widget>
<widget class="QLabel" name="label_outputfolder">
<property name="geometry">
<rect>
<x>90</x>
<y>90</y>
<width>391</width>
<height>21</height>
</rect>
</property>
<property name="text">
<string>此处显示输出的zip文件的路径</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_openbrowser">
<property name="geometry">
<rect>
<x>10</x>
<y>130</y>
<width>71</width>
<height>51</height>
</rect>
</property>
<property name="text">
<string>开启浏览器</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_login">
<property name="geometry">
<rect>
<x>90</x>
<y>130</y>
<width>71</width>
<height>51</height>
</rect>
</property>
<property name="text">
<string>登录</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_exec">
<property name="geometry">
<rect>
<x>340</x>
<y>130</y>
<width>141</width>
<height>51</height>
</rect>
</property>
<property name="font">
<font>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>下载答题情况</string>
</property>
</widget>
<widget class="QLabel" name="label_4">
<property name="geometry">
<rect>
<x>170</x>
<y>130</y>
<width>91</width>
<height>51</height>
</rect>
</property>
<property name="text">
<string>在小闲页面的
学情报告中
翻到指定页面</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
<widget class="QLabel" name="label_5">
<property name="geometry">
<rect>
<x>270</x>
<y>140</y>
<width>54</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>下载编号</string>
</property>
</widget>
<widget class="QLineEdit" name="lineEdit_num">
<property name="geometry">
<rect>
<x>260</x>
<y>160</y>
<width>71</width>
<height>20</height>
</rect>
</property>
<property name="inputMask">
<string/>
</property>
<property name="text">
<string/>
</property>
<property name="placeholderText">
<string>留空为全部</string>
</property>
</widget>
</widget>
<resources/>
<connections/>
</ui>