
Python批量下载中国大学MOOC课件
������ ������ ������������������
发布日期:2021-05-14 14:12:01
浏览次数:18
分类:精选文章
本文共 6604 字,大约阅读时间需要 22 分钟。
������������
������������������������������
pip install selenium requests
������������������������Chrome���Firefox������
# Chrome������pixel-byte install -g chromiumchromedriver# Firefox������pixel-byte install -g geckodriver
������������
from selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver������ import expected_conditions as ECfrom selenium.webdriver.common.keys import Keys��� selenium.webdriver.common.action_chains import ActionChainsfrom selenium.common.exceptions import *import requestsimport osimport time# ���������������driver = webdriver.Chrome()# driver = webdriver.Firefox()# ������������������wait = WebDriverWait(driver, 10)# ������������������def download(url, file_name): headers = { 'Host': 'hubble.netease.com', 'Origin': 'https://www.icourse163.org', 'Referer': url.split("#")[0], 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.92 Safari/537.36' } # ��������������������������������������������� if not os.path.exists(file_name) or os.path.getsize(file_name) <= 10: # ������������ with open(file_name, "wb") as f: r = requests.get(url, headers=headers, verify=False) f.write(r.content) f.close() print(f"���������������{file_name}") else: print(f"������������������{file_name}")# ������������������def get_courseware(courseware_url, path, c_range=[0, 0]): t = 0 while t < 2: try: # ������������������ driver.get(courseware_url) # ��������������������� h3 = wait.until( EC.element_to_be_clickable((By.CSS_SELECTOR, "#g-body > div.m-learnhead > div > div > div > a.f-fl > h4")) ) # ������������������ school_name = re.findall(r'/(.*?).-?', courseware_url)[0] # ������������������ h3_count = len(driver.find_elements_by_css_selector("div > div.m-learnChapterList > div.m-learnChapterNormal > div.titleBox > h3")) if c_range[1] == 0: c_range2 = h3_count else: c_range2 = c_range[1] # ������������ for index in range(3 + c_range[0], 3 + c_range2): driver.refresh() h3 = wait.until( EC.element_to_be_clickable((By.CSS_SELECTOR, "div > div.m-learnChapterList > div.m-learnChapterNormal:nth-child(3) > div.titleBox > h3")) ) h3.click() h3 = wait.until( EC.element_to_be_clickable((By.CSS_SELECTOR, "div > div.m-learnChapterList > div.m-learnChapterNormal:nth-child({}) > div.titleBox > h3".format(index))) ) h3_text = h3.text # ��������������������������� pattern = re.compile('.*?���(\d{1,3})(���|���).*.') match = re.match(pattern, h3_text) week = match.group(0) if match else h3_text # ������������������ file_count = len(driver.find_elements_by_css_selector('//div[@class="f-icon lsicon f-fl "]/span[@class="u-icon-doc"]')) print(file_count) h4_count = len(driver.find_elements_by_css_selector('div.u-learnLesson > h4')) for h4_index in range(1, h4_count + 1): h4 = wait.until( EC.element_to_be_clickable((By.CSS_SELECTOR, 'div.u-learnLesson:nth-of-type({}) > h4.j-name'.format(h4_index))) ) h4str = h4.text # ������������������������ title_count = len(driver.find_elements_by_css_selector(f'div.u-learnLesson:nth-of-type({h4_index}) > div.sourceList > div[title="������"]')) for f_index in range(1, title_count + 1): title = wait.until( EC.element_to_be_clickable((By.CSS_SELECTOR, f'div.u-learnLesson:nth-of-type({h4_index}) > div.sourceList > div[title="������"]')) ) titlestr = title.get_attribute("title") title.click() time.sleep(0.2) download_btn = wait.until( EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, '������������')) ) download_url = download_btn.get_attribute("href") # ��������������� week = week.replace(":", "-").replace("/", " ").replace("\\", " ").replace("���������", " ").replace("���", " ") titlestr = f'{h4str} {titlestr}'.replace(":", "-").replace("/", " ").replace("\\", " ").replace("���������", " ").replace("���", " ").replace("/", " ") print(f"{week} {titlestr}") file_name = os.path.join(path, f"{week} {titlestr}") download(download_url, file_name) driver.back() time.sleep(1) # ������������ h3 = wait.until( EC.element_to_be_clickable((By.CSS_SELECTOR, "div > div.m-learnChapterList > div.m-learnChapterNormal:nth-child(3) > div.titleBox > h3")) ) h3.click() h3 = wait.until( EC.element_to_be_clickable((By.CSS_SELECTOR, "div > div.m-learnChapterList > div.m-learnChapterNormal:nth-child({}) > div.titleBox > h3".format(index))) ) h3.click() t = 5 except: print("���������������������������������������������������") t += 1# ���������def main(): courseware_url = 'https://www.icourse163.org/learn/NJU-1449346162?tid=1449772442#/learn/content' path = r"E:\C&C++" get_courseware(courseware_url, path, [0, 0]) driver.quit()
������������
������������������������������
courseware_url
������������������������������path
������������������������c_range
���������������������������������������������������������
������������
- ������������������������������������������������������������
- ���������������������������������������������������
- ������������������������ SSL ������������
- ������������������������������������������
发表评论
最新留言
路过,博主的博客真漂亮。。
[***.116.15.85]2025年04月29日 19时59分58秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
C# 文本框限制大全
2021-05-15
setup facatory9.0打包详细教程(含静默安装和卸载)
2021-05-15
ionic4 路由跳转传值
2021-05-15
CSDN 怎么写出好看的博客
2021-05-15
Java基本概念:方法
2021-05-15
pwn题shellcode收集
2021-05-15
python中的序列化
2021-05-15
django中使用celery执行异步任务实现
2021-05-15
centos7 安装 mongodb3.6.3
2021-05-15
java有道翻译
2021-05-15
lora技术在无线抄表行业应用
2021-05-15
msfvenom的使用&免杀&外网渗透
2021-05-15
HTTP/2 协议详解
2021-05-15
grafana改用https登录
2021-05-15
使用jenkins进行项目的自动构建部署
2021-05-15
使用MySQLTuner-perl对MySQL进行优化
2021-05-15
2018年3月最新的Ubuntu 16.04.4漏洞提权代码
2021-05-15
异或交换两个数的值
2021-05-15
使用python绘出常见函数
2021-05-15