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

添加cheerio 应该是成功的 但是运行文件没反应

效果图这样https://img1.sycdn.imooc.com//5b5145c00001ef0d04860195.jpg

正在回答

3 回答

网页更新了,有些类名发生改变

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 = $('.course-wrap')
// [{
//     chapterTitle: '',
//     videos: [
//         title: '',
//         id: ''
//     ]
// }]
var courseData = []
chapters.each(function (item) {
var chapter = $(this)
var chapterTitle = chapter.find('h3').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 temp = video.text()
// 去除所有空格
var videoTitle = temp.replace(/\s/g, "");
var id = video.attr('href').split('video/')[1].trim()
chapterData.videos.push({
title: videoTitle,
id: id
})
})
courseData.push(chapterData)
})
return courseData
}
function printCourseInfo(courseData) {
courseData.forEach(function (item) {
var chapterTitle = item.chapterTitle.trim()
console.log(chapterTitle)
item.videos.forEach(function (video) {
console.log('\[' + video.id + '\]' + video.title)
})
})
}
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('获取课程数据出错')
})


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

因为你爬的那个网页里面的标签名有改变,所以运行没反应

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

应该是我代码写得有问题 看到别人的代码复制运行成功啦

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

举报

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

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

进入课程

添加cheerio 应该是成功的 但是运行文件没反应

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

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

帮助反馈 APP下载

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

公众号

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