課程
/前端開發
/Node.js
/進擊Node.js基礎(一)
效果圖這樣
2018-07-20
源自:進擊Node.js基礎(一) 5-10
正在回答
網頁更新了,有些類名發生改變 var?http?=?require('http') var?cheerio?=?require('cheerio') var?url?=?'http://www.xianlaiwan.cn/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('獲取課程數據出錯') })
因為你爬的那個網頁里面的標簽名有改變,所以運行沒反應
應該是我代碼寫得有問題 看到別人的代碼復制運行成功啦
舉報
本視頻教程帶你揭開Node.js的面紗,帶你走進一個全新世界
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2018-07-28
2018-07-26
因為你爬的那個網頁里面的標簽名有改變,所以運行沒反應
2018-07-20
應該是我代碼寫得有問題 看到別人的代碼復制運行成功啦