var http=require("http");
var cheerio=require('cheerio');
var url="http://www.imooc.com/learn/348";
function filterCharpter(html){
var $=cheerio.load(html);
var charpters=$(".chapter");
console.log(typeof(charpters));
var courseData=[];
charpters.each(function(item){
var chapter=$(this);
chapter.find('strong').find('.chapter-content').remove();
var chapterTitle=chapter.find('strong').text().trim();
var videos=chapter.find('.video>li');
var charpterData={
chapterTitle:chapterTitle,
videos:[]
}
videos.each(function(item){
var video=$(this);
var videoTitle=video.find('.J-media-item').text().trim();
var viedoTitleData=videoTitle.split('
');
console.log(viedoTitleData);
var id=video.data("media-id");
var viedoData={
videoTitle:viedoTitleData[0]+" "+viedoTitleData[1].trim(),
id:id
}
charpterData.videos.push(viedoData);
})
courseData.push(charpterData);
})
return courseData;
}
function printOut(courseData)
{
courseData.forEach(function(item){
console.log(item.chapterTitle+'
');
item.videos.forEach(function(video){
console.log(" 【"+video.id+"】"+ video.videoTitle);
})
})
}
var server=http.get(url,function(res){
var html="";
res.on('data',function(data){
html+=data
})
res.on('end',function(){
var courseData=filterCharpter(html);
printOut(courseData);
})
}).on('error',function(){
console.log("error")
});