為什么我的大標題是一次全部列出?
var http=require('http');
var url='http://idcbgp.cn/learn/348';
var cheerio=require('cheerio');
function filterChapters(html){
var $=cheerio.load(html);
var chapters=$('div.chapter');
var courseData=[];
chapters.each(function(data){
var chapter=$(this);
chapter.find('.icon-info').remove();
var chapterTitle=$('.chapter strong').text().trim();
var videos=chapter.find('.video').children('li');
var videolist=[];
videos.each(function(item){
var video=$(this);
var videoall=video.find('.J-media-item');
var id=videoall.attr('href').split('video/')[1];
var videoTitle=videoall.find('.icon-video').remove().end().text().trim();
videolist.push({
title:videoTitle,
id:id
})
})
courseData.push({
title:chapterTitle,
courselist:videolist
});
})
return courseData;
}
function printCourseInfo(courseData){
courseData.forEach(function(item){
var chapterTitle=item.title;
console.log(chapterTitle+'\n');
item.courselist.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);
})
})
運行時內(nèi)容為:
一、前言
二、安裝nodejs
、、
五、、
然后才是1-1,,1-2
第二章的標題處也是五章標題都列出,再是2-1,2-2,,求解~
2016-11-14
var chapterTitle=$('.chapter strong').text().trim();
bug在這兒,應更改為
var chapterTitle=chapter.$('strong').text().trim();
2022-03-26
這是回調(diào)函數(shù)用法。少寫了參數(shù),無限循環(huán)了