每个视频后面多了几个字
为什么会有一个’开始学习‘啊
为什么会有一个’开始学习‘啊
2017-11-02
var http = require('http'); var cheerio = require('cheerio'); var url = 'http://www.imooc.com/learn/348'; var chapter,chapterTitle,videos,chapterData,video; function filterChapters(html) { var $ = cheerio.load(html); var chapters = $('.chapter'); var courseData = []; chapters.each(function (item) { chapter = $(this); chapterTitle = chapter.find('strong').text().trim().replace(/\s+/g,' '); chapterTitle = chapterTitle.split(' ')[0]+' '+chapterTitle.split(' ')[1]; videos = chapter.find('.video').children('li'); chapterData = { chapterTitle: chapterTitle, videos: [] }; videos.each(function(item) { video = $(this).find('.J-media-item'); var videoTilte = video.text().replace(/[\s]/g,'').replace('开始学习', ''); var id = video.attr('href').split('video/')[1]; chapterData.videos.push({ title: videoTilte, id: id }) }); courseData.push(chapterData); }) return courseData; } function printCourseInfo(html) { var res = ''; html.forEach(function(item) { res += '标题:' + item.chapterTitle + '\n'; item.videos.forEach(function(video) { res += ' 【' + video.id + '】' + video.title + '\n'; }) }) console.log(res); } http.get(url, function(res) { var html = ''; res.on('data', function (data) { html += data; }) res.on('end', function () { var courseData = filterChapters(html); printCourseInfo(courseData); }) }).on('error',function () { console.log('出错'); })
这是我写的代码
举报