const?http?=?require('http');
const?url?=?'http://www.xianlaiwan.cn/learn/348';??
const?cheerio?=?require('cheerio');
function?filterChapters(html){
??let?$?=?cheerio.load(html);
??let?chapters?=?$('.chapter');
?
??//創建一個儲存章節的數組
??let?cunChapters?=?[];
??//遍歷章節
??chapters.each(item=>{
????let?chapter?=?$(this);
????let?chapterTitle?=?chapter.find('strong').text();
????let?chapterVideo?=?chapter.find('.video').children('li');
????//?console.log(chapterTitle);
????//單個包含視頻和章節標題的章節
????let?chapterData?=?{
??????chapterTitle:?chapterTitle,
??????chapterVideo:?[]
????}????
????chapterVideo.each(vItem=>{
??????let?video?=?$(this).find('.J-media-item');
??????let?videoTitle?=?video.text();
??????let?videoId?=?video.attr('href').split('video/')[1];
????//?console.log(videoTitle);
????//單個包含視頻標題和視頻id的video
????chapterData.chapterVideo.push({
??????videoTitle:?videoTitle,
??????videoId:?videoId
????})?
??})
????cunChapters.push(chapterData);//單個章節的集合
??});
??return?cunChapters;
};
//打印結果
function?viewHtml(cunChapters){
??cunChapters.forEach(item=>{
????//打印章節標題
????let?chapterTitle?=?item.chapterTitle;
????console.log(chapterTitle?+?'\n');
????//打印視頻
????item.chapterVideo.forEach(vItem=>{
??????//?let?videoTitle?=?vItem.videoTitle;
??????//?let?id?=?vItem.videoId;
??????//打印video標題
??????console.log('【'+?vItem.videoId?+?'】'?+?vItem.videoTitle?+?'\n');
????})
??})
}
http.get(url,function?(res){
??let?html?='';
??res.on('data',function(data){
????html?+=?data;
??});
??res.on('end',()=>{
????let?cunChapters?=?filterChapters(html);
????viewHtml(cunChapters);
??})
}).on('error',e=>{
??console.log('錯誤');
})找了一晚上
HTTP爬蟲,打印是空白
qq_嫻言蜜語_0
2017-03-24 00:47:18