課程
/前端開發(fā)
/Node.js
/進(jìn)擊Node.js基礎(chǔ)(一)
為什么會有一個’開始學(xué)習(xí)‘啊
2017-11-02
源自:進(jìn)擊Node.js基礎(chǔ)(一) 5-11
正在回答
a里面有個button標(biāo)簽,隱藏的,爬下來可以看到
<button class="r moco-btn moco-btn-red preview-btn">開始學(xué)習(xí)</button>
var?http?=?require('http'); var?cheerio?=?require('cheerio'); var?url?=?'http://idcbgp.cn/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('開始學(xué)習(xí)',?''); 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?+=?'標(biāo)題:'?+?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('出錯'); })
這是我寫的代碼
舉報
本視頻教程帶你揭開Node.js的面紗,帶你走進(jìn)一個全新世界
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網(wǎng)安備11010802030151號
購課補貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號
2018-07-29
a里面有個button標(biāo)簽,隱藏的,爬下來可以看到
<button class="r moco-btn moco-btn-red preview-btn">開始學(xué)習(xí)</button>
2017-11-02
var?http?=?require('http'); var?cheerio?=?require('cheerio'); var?url?=?'http://idcbgp.cn/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('開始學(xué)習(xí)',?''); 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?+=?'標(biāo)題:'?+?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('出錯'); })這是我寫的代碼