├── package.json ├── README.md └── app.js /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "imooc-video-download", 3 | "version": "0.0.1", 4 | "description": "可以批量下载慕课网的视频", 5 | "main": "app.js", 6 | "dependencies": { 7 | "cheerio": "^0.17.0", 8 | "superagent": "^0.20.0" 9 | }, 10 | "scripts": { 11 | "test": "node app courseid" 12 | }, 13 | "keywords": [ 14 | "imooc" 15 | ], 16 | "author": "Awai", 17 | "license": "ISC" 18 | } 19 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # imooc-video-download 2 | 3 | 该项目已经年久失修了~ 4 | 5 | 如果你有兴趣,可以 fork 之后尝试修复~ 6 | 7 | > 使用 : 8 | 9 | > 基于 Node.js 写的爬虫, 可以批量下载慕课网上的视频 10 | 11 | > - 因为基于 Node.js, 所以使用前确保已经安装好 Node.js 环境 12 | 13 | > - 这个爬虫用了 superagent 和 cheerio 这两个模块, 已经加入到项目中 14 | 15 | > - 使用前先在源码里面修改 ***储存路径*** 和 自己的 ***cookies*** 16 | 17 | 直接终端输入 18 | --- 19 | 20 | ``` bash 21 | node app id // id 就是课程id 22 | ``` 23 | `例如: http://www.imooc.com/view/442 id 就是 442` 24 | 25 | -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | /** 2 | * imooc 3 | * 4 | * Created by Awai on 15/9/14. 5 | */ 6 | 7 | /** 8 | * 修改储存目录 /xxx/xxx/ (最后要加一个 '/' ) 9 | */ 10 | var savePath = '' 11 | /** 12 | * 自己在慕课网上登录后 获取cookies 并替换下面的 13 | */ 14 | var cookies = '' 15 | 16 | var superagent = require('superagent'); 17 | var cheerio = require('cheerio'); 18 | var fs = require('fs'); 19 | 20 | var courseId = process.argv.splice(2, 1); 21 | 22 | var courseTitle = ''; 23 | var courseTotalCount = 0 24 | var finishCount = 0 25 | 26 | var download = function(url, filename, callback) { 27 | 28 | filename = filename.replace(/\(.*\)/,'') + '.mp4'; 29 | 30 | var dirPath = savePath + courseTitle + '/' 31 | if (!fs.existsSync(dirPath)) { 32 | fs.mkdirSync(dirPath); 33 | } 34 | 35 | console.log('开始下载第' + courseTotalCount + '个视频' + filename + ' 地址: ' + url); 36 | var writeStream = fs.createWriteStream(dirPath + filename); 37 | writeStream.on('close', function() { 38 | 39 | callback(filename); 40 | }) 41 | 42 | var req = superagent.get(url) 43 | req.pipe(writeStream); 44 | 45 | } 46 | 47 | var getMovieUrl = function(id, callback) { 48 | superagent.get('http://www.imooc.com/course/ajaxmediainfo/?mid=' + id + '&mode=flash') 49 | .end(function(err, res) { 50 | var url = JSON.parse(res.text); 51 | 52 | if(url.result == 0) { 53 | url = url.data.result.mpath[0]; 54 | callback(url); 55 | } 56 | }) 57 | } 58 | 59 | var headers = { 60 | "Cache-Control": "max-age=0", 61 | "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", 62 | "Referer": "http://www.imooc.com/", 63 | "Accept-Encoding": "gzip, deflate, sdch", 64 | "Accept-Language": "zh-CN,zh;q=0.8", 65 | "Cookie": cookies 66 | }; 67 | 68 | superagent 69 | .get('www.imooc.com/learn/' + courseId) 70 | .set(headers) 71 | .end(function(err, res) { 72 | 73 | var $ = cheerio.load(res.text); 74 | 75 | $('.course-infos .hd').find('h2').each(function(item) { 76 | courseTitle = $(this).text(); 77 | }) 78 | 79 | $('.chapter').each(function(item) { 80 | 81 | var videos = $(this).find('.video').children('li') 82 | 83 | videos.each(function(item) { 84 | var video = $(this).find('a') 85 | var filename = video.text().replace(/(^\s+)|(\s+$)/g,""); 86 | var id = video.attr('href').split('video/')[1] 87 | if (!id) { 88 | console.log('跳过下载: ' + filename) 89 | }else { 90 | 91 | // 获取下载地址 92 | getMovieUrl(id, function(url) { 93 | 94 | courseTotalCount ++; 95 | 96 | // 开始下载 97 | download(url, filename, function(_filename) { 98 | finishCount++; 99 | console.log('第' + finishCount + '个视频' + _filename + '下载完成') 100 | }) 101 | }) 102 | } 103 | }) 104 | }) 105 | }) 106 | --------------------------------------------------------------------------------