超星学习通作业下载
学习通有个很狗血的功能,作业上传成功以后,没有批改前就不能下载.琢磨半天写出个小玩意儿,大佬勿喷,多多指教!!!:lolimport re
import requests
import pandas as pd
class ChaoXing:
def __init__(self, user, password):
self.password = password
self.user = user
self.work_data_list = []
self.class_lists = []
self.headers = {
"User-agent": "Mozilla/4.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36",
}
self.session = requests.session()
def session_token(self):
login_api = "https://passport2.chaoxing.com/api/login"
params = {
"name": self.user,# 学习通账号
"pwd": self.password,# 学习通密码
"verify": "0",
"schoolid": "",
}
self.session.get(login_api, params=params, headers=self.headers)
def class_list(self):
class_url = "http://mooc2-ans.chaoxing.com/visit/courses/list?v=1652629452722&rss=1&start=0&size=500&catalogId=0&searchname="
class_data = self.session.get(url=class_url,headers=self.headers).text.replace(' ',"")
obj = re.compile(r'"color1" href="(?P<src>.*?)".*?break-all;" title="(?P<name>.*?)">.*?<i class="icon-news"></i>',re.S)
result = obj.finditer(class_data)
for cx in result:
dict={}
dict['src']=cx.group("src")
dict['name']=cx.group("name")
self.class_lists.append(dict)
def class_data(self):
pf = pd.DataFrame(self.class_lists)
pf.index = pf.index + 1
data_dicts = {
"name": "课程名称",
}
pf.rename(columns=data_dicts, inplace=True)
print(pf)
a = int(input("请输入课程序号:"))
src = (self.class_lists["src"])
class_res = self.session.get(url=src,headers=self.headers).text
work_url = re.findall(r'title="作业" data-url="(.*?)">',class_res,re.S)
courseid = re.findall(r'name="courseid" value="(.*?)"/>',class_res,re.S)
classid = re.findall(r'name="clazzid" value="(.*?)"/>', class_res, re.S)
workenc = re.findall(r'name="workEnc" value="(.*?)">', class_res, re.S)
self.work_class_url = work_url + "?courseId=" + courseid +"&classId=" + classid +"&enc="+workenc
work_class_url_res = self.session.get(url=self.work_class_url,headers=self.headers).text
works_url = re.findall(r'<li onclick="goTask.*?data="(.*?)">',work_class_url_res,re.S)
for i in works_url:
page_text = self.session.get(url=i,headers=self.headers).text
if "span data="" in page_text:
obj = re.compile(r'<h2 class="mark_title">(?P<name>.*?)</h2>.*?data="(?P<enc>.*?)" type="(?P<type>.*?)"',re.S)
else:
obj = re.compile(r'<h2 class="mark_title">(?P<name>.*?)</h2>.*?<p class="attachNew"><em style="font-style:normal; display:block;min-height:2px;"></em><span data="(?P<enc>.*?)" type="(?P<type>.*?)" name=".*?"',re.S)
class_result = obj.finditer(page_text)
for j in class_result:
dict={}
dict['enc'] = j.group("enc")
dict['type'] = j.group("type")
dict['file_name'] = (j.group("name")+"."+j.group("type")).replace(r"、",".")
self.work_data_list.append(dict)
pf = pd.DataFrame(self.work_data_list)
pf.index = pf.index + 1
data_dicts = {
"file_name": "文件名称",
}
pf.rename(columns=data_dicts, inplace=True)
print(pf)
def download_data(self):
work_nums = int(input("请输入你想下载文件的序号:"))
work_enc = self.work_data_list['enc']
work_type = self.work_data_list['type']
work_name = self.work_data_list['file_name']
work_url = "https://mooc1.chaoxing.com/ueditorupload/read?objectId=" + work_enc
headers = {
'Referer': self.work_class_url,
"User-agent": "Mozilla/4.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36",
}
if work_type == 'zip':
work_content = self.session.get(url=work_url,headers=headers,stream=True)
else:
work_res = self.session.get(url=work_url, headers=self.headers).text
file_url = re.findall(r'<a class="btnDown" href="(.*?)"><span>',work_res,re.S)
work_content = self.session.get(url=file_url, headers=headers ,stream=True)
work_path = "./" + work_name
with open(work_path, 'wb') as f:
for chunk in work_content.iter_content(1024 * 1024 * 2):
f.write(chunk)
if __name__ == "__main__":
chaoxing = ChaoXing(user="", password="")
chaoxing.session_token()
chaoxing.class_list()
chaoxing.class_data()
chaoxing.download_data()
nianboy 发表于 2022-5-17 10:31
chaoxing = ChaoXing(user="", password="")在这里输入账号密码然后运行就可以啦.
嗯嗯,这部试过了,会列出课程列表,有的课程可以正常显示作业,但是有的课程提示这个
Empty DataFrame
Columns: []
Index: []
请输入你想下载文件的序号: BlueAir 发表于 2022-5-17 11:07
好的,找到了,谢谢你,还有问题就是 有的课程无法获取作业列表,有的获取到作业列表,但是下下载报 ...
后来又试了一下,应该是只能识别doc,和txt 类别的,视频和图片识别不了:loveliness::loveliness::loveliness: 多给点免费的评分吧,大佬们:lol 好啊,有了这个再也不用等了。 看看怎么样 这是个好东西,收藏备用 看看,学习一下 厉害了,能推荐给同学吗? 看看谢谢楼主分享 ModuleNotFoundError: No module named 'requests'
这是用什么版本写的?运行时出现了上面的代码。 666 虽然已经用不到了