运行出现一大段空白,但是没有打印,自己盯着代码看了好几遍,实在没发现错误,求解答
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=[];// save every chapter
chapters.each(function(item){
var chapter=$('this');
var chapterTitle=chapter.find('strong').text();
var videos=chapter.find('.video').children('li');
var chapterData={
title:chapterTitle,
videos:[]
};//save a chapter
videos.each(function(item){
var video=$(this).find('.J-media-item');
var videoTitle=video.text();
console.log(videoTitle);
var videoId=video.attr('href').split('video/')[1];
chapterData.videos.push({
title:videoTitle,
id:videoId
});
})
//console.log('222');
courseData.push(chapterData);
});
return courseData;
}
function print(courseData){
courseData.forEach(function(item){
console.log(item.title+'\n');
item.videos.forEach(function(video){
console.log(video.title+':'+video.id+'\n');
});
});
};
http.get(url,function(res){
var html='';
res.on('data',function(data){
html+=data;
})
res.on('end',function(){
var courseData=filterChapters(html);
print(courseData);
})
}).on('error',function(){
console.log('http error');
})