87,907
社区成员
发帖
与我相关
我的任务
分享
var Promise = require('bluebird')
var request = Promise.promisify(require("request"));
this.getPageHtml = function(pageUrl) {
var tryGetPageHtml = function(pageUrl, options, totalTimes) {
return request.getAsync(options).spread(function(response, body) {
if (response.statusCode == 200)
return body
else throw new Error(response.statusCode)
}).catch(function(err) {
console.log('catch')
if (totalTimes == 0) {
return Promise.reject()
}
else {
totalTimes -= 1
return Promise.delay(200).then(function() {
return tryGetPageHtml(pageUrl, options, totalTimes)
})
}
})
}
//console.log(pageUrl)
var options = {
url: pageUrl,
followRedirect: true,
jar: this.auth.cookies, // 保存cookie
timeout: 4000,
headers: {},
}
var totalTimes = 10
return tryGetPageHtml(pageUrl, options, totalTimes).bind(this)
}