今天干了件有点快乐的事情——批量下载MOOC课件
代码搬运,需做以下修改
1.pip install selenium
2.chormedriver下载7.2可
2.修改courseware_url

# coding: utf-8from 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
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.common.exceptions import *
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarningrequests.packages.urllib3.disable_warnings(InsecureRequestWarning)
requests.adapters.DEFAULT_RETRIES = 5
import time
import os
import redriver = 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("\t下载成功:{}".format(file_name))else:print("\t文件已存在:{}".format(file_name))# 课件地址  存储路径  范围[a, b](第a章到第b章,默认[0, 0]表示全部)
def get_courseware(courseware_url, path, c_range=[0, 0]):t = 0while 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'/([a-zA-Z]+)-', courseware_url)[0]title = h3.textpath_1 = os.path.join(path, title + "_" + school_name)if not os.path.exists(path_1):os.makedirs(path_1)path = os.path.join(path_1, "courseware")if not os.path.exists(path):os.makedirs(path)# 总章节数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_countelse: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.textprint("{}:".format(h3_text), end="\t")patten = re.compile('.*?第(.{1,3})(周|章).*?')match = re.match(patten, h3_text)if match:week = match.group(0)else:week = h3_texth3.click()time.sleep(3)#                 file_count = len(driver.find_elements_by_xpath('//div[@class="f-icon lsicon f-fl "]/span[@class="u-icon-doc"]'))file_count = len(driver.find_elements_by_xpath('//div[@class="sourceList"]/*[@title="文档讲稿"]'))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))))# 标题4h4str = h4.textfile_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, file_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}'title = titlestr.replace(":", "-").replace("/", " ").replace("\\", " ").replace("课件:"," ").replace(":", " ").replace("/", " ")print(week, "   ", title)file_name = path + "\\" + week + " " + "".join(title.split()).replace(":", " ") + "." + \download_url.split(".")[-1].split('&')[0]print(file_name)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 = 5except FileNotFoundError:print("FileNotFoundError: [Errno 2] No such file or directory: ")t += 1def main():courseware_url = 'https://www.icourse163.org/learn/XDU-1001638014?tid=1462808447#/learn/content'path = r"D:\大二下\信号与系统\中国大学MOOC"# 课件地址  存储路径  范围[a, b](第a章到第b章,默认[0, 0]表示全部)get_courseware(courseware_url, path, [0, 0])driver.quit()  # 退出浏览器if __name__ == '__main__':main()

question:
1.每节只能下一个课件,应该有多少下多少
2.命名格式调一下更好,放假了来改进

Python批量下载MOOC课件相关推荐

  1. python调用默认播放器_用Python批量下载MOOC资源

    简介 这是我编写的一个Python脚本,用来批量下载一门MOOC的资源,包括视频.PDF和课程目录. 你是否也曾上MOOC学习过?有没有和我一样遇到这些情况:在线看MOOC视频消耗流量好多,或者浏览器 ...

  2. python下载网页里面所有的图片-Python批量下载网页图片详细教程

    很多朋友在网上查找批量下载图片的方法~发觉挺凌乱的,无从下手.这里绿茶小编就来跟大家分享下使用Python批量下载图片方法. 目标:爬取某个网站上n多页的链接,每个链接有n多张图片,每一页对应一个文件 ...

  3. 群里又会python的吗_自从会了Python在群里斗图就没输过,Python批量下载表情包!...

    原标题:自从会了Python在群里斗图就没输过,Python批量下载表情包! 导语 最近图慌,于是随便写了个表情包批量下载的脚本,没什么技术含量,纯娱乐性质. 让我们愉快地开始吧~ 开发工具 Pyth ...

  4. python批量下载b站_python 批量下载bilibili视频的gui程序

    运行效果: 完整代码: # !/usr/bin/python # -*- coding:utf-8 -*- # time: 2019/07/02--08:12 __author__ = 'Henry' ...

  5. Python 批量下载SIGMOD,VLDB的论文 Mac OS

    这里写自定义目录标题 Python 批量下载SIGMOD,VLDB的论文 Mac OS 实现 0.要爬取的网站 1.下载单篇论文 2.获得所有论文的链接 完整代码 Python 批量下载SIGMOD, ...

  6. 教你怎么使用python批量下载图片

    教你怎么使用python批量下载图片 文章目录 教你怎么使用python批量下载图片 前言 一.运行环境 1. win10 2. python==3.7.2 二.需要用到的参数 1. download ...

  7. python批量下载模库网图片

    这里写自定义目录标题 python批量下载模库网图片 步骤: 代码 python批量下载模库网图片 步骤: 获取页数 获取列表页 获取图片链接和名字相关字典 创建存放图片的文件夹 下载图片 代码 im ...

  8. python怎么批量下载年报_使用Python批量下载Wind数据库中的PDF报告

    原标题:使用Python批量下载Wind数据库中的PDF报告 背景 最近小编出于工作需要,准备在Wind金融数据终端批量下载上市公司2019年第一季度业绩预告.通过相关的条件检索,发现其相关数据有近百 ...

  9. 使用Python批量下载哨兵一号(sentinel-1)的精密轨道数据(precise orbit data)

    使用Python批量下载哨兵一号(sentinel-1)的精密轨道数据 1. 安装Python及data_downloader包 2. 下载精密轨道数据与辅助数据 1. 安装Python及data_d ...

  10. 用Python批量下载DACC的MODIS数据

    本人初次尝试用Python批量下载DACC的MODIS数据,记下步骤,提醒自己,数据还在下载,成功是否未知,等待结果中...... 若有大佬发现步骤有不对之处,望指出,不胜感激. 1.下载Python ...

最新文章

  1. wukong引擎源码分析之搜索——docid有序的数组里二分归并求交集,如果用跳表的话,在插入索引时会更快...
  2. 【转】计算机中浮点数的表示
  3. java socket字符串_Java Socket Bug:从Socket的InputStream读取字符串
  4. 数据库大战,AWS又将目标瞄准了微软SQL Server
  5. 逸出 java_【java】知识系谱-基础篇-线程-发布、逸出
  6. Scilab 求解线性方程组示例(linsolve)
  7. 6D位姿估计算法Densefusion代码阅读
  8. python urlretrieve_使用urllib库的urlretrieve()方法下载网络文件到本地的方法
  9. Linux Shell 使用技巧
  10. 关于websocket 在生产环境中遇到的问题 及 解决办法
  11. Django MTV - 模型层 - (专题)知识要点与实战案例
  12. 新云php修改,MySQL_新云CMS防采集的代码修改,列表页修改: 打开INC目录下Ne - phpStudy...
  13. Unity3D丨面试题目
  14. 如何编写Firefox扩展
  15. hive、hadoop面试题
  16. CMD 命令行实现 Windows 下复制文件到文件夹下的所有文件夹
  17. win10 计算机网络密码,详细教你Win10怎么查看无线网络密码
  18. oracle12c 配置监听,redhat上oracle 12c配置监听
  19. c语言 教学设计,C语言教案
  20. Java基础知识(七) 输入输出流

热门文章

  1. 【工具】中国菜刀 官方原版下载 官网下载链接
  2. 一步步教你写一份优秀的软件测试简历(带样例)
  3. 杨中科:我的大学生活
  4. Notes Ninth Day-渗透攻击-红队-打入内网
  5. 神经网络控制系统设计,神经网络技术及其应用
  6. 2021年12月最新大数据白皮书(附下载)
  7. .Net C# 微信APP支付的开发步骤
  8. 地市级地铁数据管理信息系统解决方案
  9. Unity TextMeshPro显示中文
  10. Enviropro EP100D-08管式土壤水分探针