寫完后運行報錯
TypeError: the promise constructor cannot be invoked directly
??? See http://goo.gl/MqrFmX
??? at check (E:\imooc\http\node_modules\bluebird\js\release\promise.js:62:15)
??? at Array.Promise (E:\imooc\http\node_modules\bluebird\js\release\promise.js:72:9)
??? at Object.<anonymous> (E:\imooc\http\promise_crawler.js:94:6)
??? at Module._compile (module.js:570:32)
??? at Object.Module._extensions..js (module.js:579:10)
??? at Module.load (module.js:487:32)
??? at tryModuleLoad (module.js:446:12)
??? at Function.Module._load (module.js:438:3)
??? at Module.runMain (module.js:604:10)
??? at run (bootstrap_node.js:394:7)
??? at startup (bootstrap_node.js:149:9)
??? at bootstrap_node.js:509:3
想知道為什么不能調用
2017-08-20
你這代碼里面大量語法錯誤,很多變量沒有var ,而且沒用;用的,
2017-07-10
最下面一部分 promise.all 不是promise.call額。。。。
2017-04-09
var http = require('http');
var cheerio = require('cheerio');
var Promise = require('bluebird');
var baseUrl = 'http://www.xianlaiwan.cn/learn/';
var url = 'http://www.xianlaiwan.cn/learn/348';
var videIds = [728, 637, 348, 259, 197, 134, 75];
function filterChapters(html) {
?var $ = cheerio.load(html),
??chapters = $('.chapter'),
??????? title = $('.hd .cleadrfix h2').text();
??????? number = $('.meta-value .js-learn-num').text();
?
??courseData = {
??????????? title: title,
??????????? number: number,
??????????? videos : []
??????? }
?
?chapters.each(function (item) {
??var chapter = $(this),
???chapterTitle = chapter.find('strong').text(),
???videos = chapter.find('.video').children('li'),
???chapterData = {
????chapterTitle: chapterTitle,
????videos: []
???};
??videos.each(function (item) {
???var video = $(this).find('.J-media-item'),
????videoTitle = video.text(),
????id = video.attr('href').split('video/')[1];
???
???chapterData.videos.push({
????title: videoTitle,
????id: id
???});
??});
??
??courseData.videos.push(chapterData);
?});
?
?return courseData;
}
function printCourseInfo(coursesData) {
??? coursesData.forEach(function(courseData)? {
??????? console.log(courseData.number + '人學過' + couresData.title + '\n');
??? })
???
?coursesData.forEach(function (coursesData) {
??????? 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');
??????????? });
??????? })
??
?});
}
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 () {
???????????????
??????????????? resolve(html);
???????????????
??????????? });
??????? }).on('error', function (e) {
???????????
??????????? reject(e)
??????????? console.log('獲取課程數據出錯');
??????? });
??? })
}
var fetchCourseArray = [];
videIds.forEach(function(id) {
??? fetchCourseArray.push(getPageAsync(baseUrl + id))
})
Promise
??? .call(fetchCourseArray)
??? .then(function(pages) {
??????? var coursesData = [];
???
???
??????? pages.forEach(function(html) {
??????????? var courses = filterChapters(html);
??????????? coursesData.push(courses);
??????? })
??????? coursesData.sort(function(a, b) {
??????????? return a.number < b.number;
??????? })
???????
??????? printCourseInfo(courseData);
??? })