第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定

只輸出正在爬取xxx頁面,實際沒拿到數(shù)據(jù)怎么回事兒,哪里錯了?

var http = require('http');

var Promise = require('bluebird');

var cheerio = require('cheerio');

var baseUrl = 'http://idcbgp.cn/learn/';

var videoIds=[259,197];


/*解析html,提取課程有用信息*/

function filterChapers(html){

var $ = cheerio.load(html);//把html內(nèi)容裝載進(jìn)來

var chapters = $('.chapter');//通過類名拿到所有的章節(jié)

var title = $('.course-infos .path span').text()

? ? var number = parseInt($($('.meta-value strong')[3]).text().trim(), 10)

console.log('title: '+title); ??

/*courseData = {

title:title,//課程的名稱

number:number,//學(xué)習(xí)過的人數(shù)

videos: [{

? chapterTitle:'',//每一章的名字

? videos:[ //每一章很多小節(jié),即很多個視頻

? ?title:'',

? ?id:''

? ]

}]

}*/


//預(yù)備的課程數(shù)組

var courseData = {

title: title,

number: number,

videos: []

};


//每一章進(jìn)行遍歷

chapters.each(function(item){

//這里是單獨的每一章

var chapter = $(this);

var chapterTitle = chapter.find('strong').text(); //這一章的名字,如 第一章:前言

console.log(chapterTitle)

var videos = chapter.find('.video').children('li');//這一章的所有小節(jié)

//定義一個存放 章 的內(nèi)容

var chapterData = {//這一章的內(nèi)容對象

chapterTitle:chapterTitle, ? //章的名字

videos:[] ? ? ? ? ? ? ? ? ? ?//這一章的所有小節(jié)

}


videos.each(function(){

var video = $(this).find('.studyvideo');

var videoTitle = video.text();

var id = video.attr('href').split('video/')[1];//拿到當(dāng)前小節(jié)即這小節(jié)視頻的id


chapterData.videos.push({

title : videoTitle,

id : id

})

})//這一章的所有小節(jié)內(nèi)容結(jié)束了


courseData.videos.push(chapterData)//當(dāng)前這一章分析后的內(nèi)容添加到課程數(shù)組中

})


return courseData;

}


/*

? ?打印課程信息

*/

function printCourseInfo(coursesData){


coursesData.forEach(function(courseData){

console.log(courseData.number + ' 人學(xué)過 '+ courseData.title+'\n');

})


coursesData.forEach(function(courseData){


console.log('### ' + courseData.title+'\n');


courseData.videos.forEach(function(item){

var chapterTitle = item.chapterTitle;


console.log(chapterTitle + '\n');

item.videos.forEach(function(video){

console.log('【'+video.id+'】' + video.title+'\n');

})

})

})

}


// 異步爬取頁面html

function getPageAsync(url){

return new Promise(function(resolve,reject){

console.log('正在爬取 '+url);


http.get(url,function (res) {

var html = '';


res.on('data',function(data){

html += data;

})


res.on('end',function(){

console.log('爬取' + url + '成功');

resolve(html);//通過resolve傳遞下去

})


}).on('error',function(){

reject(e);//爬蟲在爬的時候出錯了,返回一個錯誤信息

console.log('爬取失敗');

})

})

}


// 爬取到的html頁面集合

var fetchCourseArray = [];


// 批量爬取課程頁面

videoIds.forEach(function(id){

fetchCourseArray.push(getPageAsync(baseUrl+id));

})


/*

all里面是一個個的promise對象

*/

Promise

.all([fetchCourseArray])

.then(function(pages){

var coursesData = [];


pages.forEach(function(html){

// 提取課程信息

var courses = filterChapers(html);

coursesData.push(courses);

})


coursesData.sort(function(a,b){

return a.number < b.number;//從大到小排序

})

// 打印課程信息

printCourseInfo(coursesData);

})





正在回答

1 回答


/*
all里面是一個個的promise對象
*/
Promise
.all([fetchCourseArray])??? //<------這里不應(yīng)該有中括號


1 回復(fù) 有任何疑惑可以回復(fù)我~
#1

陌上09 提問者

謝謝,我找到了
2016-08-27 回復(fù) 有任何疑惑可以回復(fù)我~

舉報

0/150
提交
取消
進(jìn)擊Node.js基礎(chǔ)(二)
  • 參與學(xué)習(xí)       76728    人
  • 解答問題       242    個

本教程帶你攻破 Nodejs,讓 JavaScript流暢運行在服務(wù)器端

進(jìn)入課程

只輸出正在爬取xxx頁面,實際沒拿到數(shù)據(jù)怎么回事兒,哪里錯了?

我要回答 關(guān)注問題
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號