2018.10.5 最新代码可用
var http = require('http')
var cheerio = require('cheerio')
var url = 'http://www.imooc.com/learn/637'
function filterChapters(html) {
var $ = cheerio.load(html)
var chapters = $('.chapter')
var courseData = []
function trim(str) {
return str.replace(/[\r\n ]/g, "") //去除字符算中的空格
}
chapters.each(function (index, item) {
var chapter = $(item)
var chapterTitle = chapter.find('h3').text().trim()
var videos = chapter.find('.video').children('li')
var chapterData = {
chapterTitle: chapterTitle,
videos: []
}
// console.log(chapterTitle);
videos.each(function (index, item) {
var video = $(item).find('.J-media-item')
var videoTitle = video.text()
videoTitle = trim(videoTitle)
var id = video.attr('href').split('video/')[1]
// console.log(videoTitle);
chapterData.videos.push({
videoTitle: videoTitle,
id: id
});
});
courseData.push(chapterData)
})
return courseData
}
function printCourseInfo(courseData) {
courseData.forEach(function (item) {
var chapterTitle = item.chapterTitle
console.log(chapterTitle + '\n')
item.videos.forEach(function (video) {
console.log('【' + video.id + '】' + video.videoTitle + '\n')
})
})
}
http.get(url, function (res) {
var html = ''
res.on('data', function (data) {
html += data
})
res.on('end', function () {
// console.log(html)
var courseData = filterChapters(html)
printCourseInfo(courseData);
})
}).on('error', function () {
console.log('获取课程信息出错!')
})