Promise.all实现多个同步的异步

1.创建路由,使用phantomjs抓取页面
const BookInfo = require(‘./containers/BookInfo’);
[code]router.get(‘/bookInfo’, async (ctx, next) => {
//var keys = escape(ctx.request.body.val);
var bid = ctx.request.query.bid;
var authorId = ctx.request.query.authorId;

let data = {};
//获取小说详细信息
let nowBook = function(){ return BookInfo.getBookinfo({
url: encodeURI(`https://book.qidian.com/info/${bid}`),
userAgent: “Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36”
});
}
//获取小说其他书籍
let otherBook = function(){ return BookInfo.getOtherBook({
url: encodeURI(`https://my.qidian.com/author/${authorId}`),
userAgent: “Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36”
});
}

var p1 = new Promise(function(resolve, reject) {
let ddd = BookInfo.getBookinfo({
url: encodeURI(`https://book.qidian.com/info/${bid}`),
userAgent: “Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36”
});
resolve(ddd)
});
var p2 = new Promise(function(resolve, reject) {
let ddd = BookInfo.getOtherBook({
url: encodeURI(`https://my.qidian.com/author/${authorId}`),
userAgent: “Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36”
});
resolve(ddd)
});
let results = await Promise.all([p1, p2]);
console.log(results)
data = results[0].result;
data.authorBooks = results[1].result;

ctx.body = {
status: 1,
data: data
}

});[/code]

2.使用phantomjs抓取
[code]const phantom = require(‘phantom’);

const getBookinfo = async (ops)=> {
console.log(Date.now())
let data = {};
//创建实例
let instance = await phantom.create();
//创建页面容器
let page = await instance.createPage();
//设置
page.setting(“userAgent”, ops.userAgent)
//判断是否访问成功
let status = await page.open(ops.url),
code = 1;
if (status !== ‘success’) {
//访问失败修改状态码
code = -1;
} else {
//获取当前时间
var start = Date.now();

var result = await page.evaluate(function() {
///获取最新的讨论
var commentCon = [];
$(‘.discuss-list li.cf’).each(function(i, elem) {
var bb = $(elem).find(‘.discuss-info’);
commentCon.push({
ico: “https:”+$(elem).find(‘.user-photo img’).attr(‘src’)+”.png”,
name: bb.find(‘.blue’).text(),
//xin: (bb.find(‘.score-min’).attr(‘class’)).split(‘star’)[1],
extend: bb.find(‘h5 a’).text(),
time: bb.find(‘.info ‘).children(‘span’).text()
})

});
return ({
introduce: $(‘.book-content-wrap .book-intro p’).text(),
//评论数
comment: parseInt($(‘#J-discusCount’).text().split(“(“).join(“”)),
/////前15条评论内容
commentCon: commentCon,
ptotal: parseInt($(‘#J-catalogCount’).text().split(“(“).join(“”))
})
})
data = {
status: code,
url: ops.url,
time: Date.now() – start,
result: result
}
}
//退出实例
await instance.exit();

return data;
};

const getOtherBook = async (ops)=> {
console.log(Date.now())
let data = {};
//创建实例
let instance = await phantom.create();
//创建页面容器
let page = await instance.createPage();
//设置
page.setting(“userAgent”, ops.userAgent)
//判断是否访问成功
let status = await page.open(ops.url),
code = 1;
if (status !== ‘success’) {
//访问失败修改状态码
code = -1;
} else {
//获取当前时间
var start = Date.now();
var result = await page.evaluate(function() {
return $(‘.author-work .author-item’).map(function() {
return ({
id: $(this).find(‘.author-item-title a’).attr(‘data-bid’),
workTime: $(this).find(‘.author-item-time’).text(),
imgUrl: $(this).find(‘.author-item-book img’).attr(‘src’),
name: $(this).find(‘.author-item-title a’).text(),
introduce: $(this).find(‘.author-item-content’).text(),
pageNumbe: $(this).find(‘.author-item-exp’).text(),
nowPage: $(this).find(‘.author-item-update a’).text(),
nowTime: $(this).find(‘.author-item-update span’).text()
})
}).toArray();
})
data = {
status: code,
url: ops.url,
time: Date.now() – start,
result: result
}
}
//退出实例
await instance.exit();

return data;
};

module.exports = {
getBookinfo: getBookinfo,
getOtherBook: getOtherBook
}[/code]

发表评论

邮箱地址不会被公开。 必填项已用*标注