慕课网教程相关代码
var http = require('http');
var cheerio = require('cheerio');
var url = 'http://www.imooc.com/learn/348';
var assert = require('assert');
function deleteBlank(string) {
results = []
lines = string.split('\r\n');
lines.forEach(function (line) {
if(! /^\s*$/.test(line)) {
line = line.replace(/(^\s*)|(\s*$)/g, '');
results.push(line);
}
});
return results.join(' ');
}
function filterChapters(html) {
// [{
// chpaterTitle: '',
// videos: [{
// title: '',
// id: ''
// }]
// }]
var $ = cheerio.load(html);
var chapters = $('.chapter ');
var courseData = [];
chapters.each(function(data) {
var chapter = $(this);
var chapterTitle = deleteBlank(chapter.find('strong').text());
var videos = chapter.find('ul.video').children('li');
var chapterData = {
chapterTitle: chapterTitle,
videos: []
}
var video = null;
var videoTitle = null;
var id = null;
videos.each(function(item) {
video = $(this).find('a.J-media-item');
button = video.find('button');
button.remove(); // 删除掉button节点,就没有开始学习的文本节点了。
videoTitle = deleteBlank(video.text());
id = video.attr('href').split('video/')[1];
chapterData.videos.push({
title: videoTitle,
id: id
})
})
courseData.push(chapterData)
});
return courseData;
}
function printCourseInfo(courseData) {
courseData.forEach(function(course) {
var chapterTitle = course.chapterTitle;
console.log(chapterTitle);
course.videos.forEach(function(video) {
console.log(' 【' + video.id + '】' + video.title);
});
})
}
http.get(url, function(res) {
var html = ''
res.on('data', function(data) {
html += data;
});
res.on('end', function() {
var courseData = filterChapters(html);
printCourseInfo(courseData);
})
}).on('error', function() {
console.log('获取数据课程出错哦!');
});