var http=require('http');
var cheerio=require('cheerio');
var url='http://www.imooc.com/learn/348';
function filterChapters(html){
var $=cheerio.load(html);
var chapters=$('.chapter ');
var courseData=[];
chapters.each(function(item){
var chapter=$(this);
chapter.find('strong').find(':nth-child(n)').remove(); //使用此方法,将获取到的子节点的内容移除掉
var chapterTitle=chapter.find('strong').text().replace(/\s+/g,"");
var videos=chapter.find('ul').children('li');
var chapterData={
chapterTitle:chapterTitle,
videos:[]
}
videos.each(function(item){
var video=$(this).find('a');
var len=video.text().replace(/\s+/g,"").length;
var videoTitle=video.text().replace(/\s+/g,"").substr(0,len-4);
var 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(item){
var chapterTitle=item.chapterTitle;
console.log(chapterTitle+ '\n');
item.videos.forEach(function(video){
console.log('【'+video.id+'】'+ video.title+'\n');
})
})
}
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(e){
console.log(e);
});