为了账号安全,请及时绑定邮箱和手机立即绑定

最后一节课的输出有问题


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 = $('.learnchapter');
	/*
	[{
		chapterTitle: '',
		videos: [
			title: '',
			id: ''
		]
	}]
	*/
	var courseData = [];
	chapters.each(function(item){
		var chapter = $(this);
		chapterTitle = chapter.find('strong').text();
		var videos = chapter.find('.video').children('li');
		var chapterData = {
			chapterTitle: chapterTitle,
			videos: []
		}

		videos.each(function(item){
			// var video = $(this).find('.studyvideo');
			var video = chapter.find('.studyvideo');
			var title = video.text();
			var id = video.attr('href').split('video/')[1];

			chapterData.videos.push({
				title: title,
				id: id
			})
		});

		courseData.push(chapterData);
	});

	return courseData;
}

function printCourseInto(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('data',function(data){
		var courseData = filterChapters(data);
		printCourseInto(courseData);
	})
}).on('error',function(e){
	console.log("Got error:" + e.message);
});


正在回答

2 回答

//适合现在慕课网的代码,水平有限,有不足希望指点
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')
//  [
//      {
//          chapterTitle:'';
//          videos:[
//              title:''
//              id:''
//          ]
//      }
//  ]
// }
    var courseData=[]
    chapters.each(function(item){
        $('div').remove('.chapter-content')
        $('button').remove()
        var chapter = $(this)
        var chapterTitle =chapter.find('strong').text()
        var videos = chapter.find('.video').children('li')
        var chapterData = {
            chapterTitle: chapterTitle,
            videos: []
        }
        videos.each(function(item){
            var video = $(this).find('.J-media-item')
            var videoTitle = video.text().replace(/\s+/g,' ')
            var id =video.attr('href').split('video/')[1]
            chapterData.videos.push({
                title : videoTitle,
                id: id
            })
        })
        courseData.push(chapterData)
    })
    return courseData;
}
function printCourseInfo(courseData){
    var info = ''
    courseData.forEach(function(item){
        info += item.chapterTitle.replace(/\s/g,' ').trim() + '\n'
        item.videos.forEach(function(video){
            info += ' 【'+video.id.replace(/\s/g,' ') +'】   '+ video.title.replace(/\s/g,' ').trim() +'\n'
        }) 
    })
    console.log(info)
}
console.log('\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)
        over();
    })
}).on('error',function(){
    console.log('获取数据出错!')
})
function over(){
    console.log('\n                     爬虫程序爬取结束~~~')
}


0 回复 有任何疑惑可以回复我~

浏览器审查元素看一下,是不是类名称换掉了。

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
进击Node.js基础(一)
  • 参与学习       219315    人
  • 解答问题       982    个

本视频教程带你揭开Node.js的面纱,带你走进一个全新世界

进入课程

最后一节课的输出有问题

我要回答 关注问题
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号