异步的node怎么打印出最后的结果,保证不是空的
node新手爬虫,主要是异步流程的控制不是很熟练。 getTitleHref()函数是获取标题的链接,然后getLink()模拟进入了改标题的链接去获取到电影的bt链接种子。
现在我想打印出最后的ans结果,由于打印操作是同步的,直接打印会是空的,该怎么解决😂
const cheerio = require('cheerio');
const http = require('http');
const iconv = require('iconv-lite');
let baseUrl = "http://www.ygdy8.net/html/gndy/dyzz/list_23_";
let Host = "http://www.ygdy8.net/";
const totalPage = 2; //指定爬多少页数据
let ans = [];
//获取页面电影数据
function getTitleHref(url,page) {
let startUrl = url+page+".html";
http.get(startUrl,function(res) {
const { statusCode } = res;
let chunks = [];
res.on('data',function(chunk){
chunks.push(chunk);
});
res.on('end',function(){
let title = [];
let html = iconv.decode(Buffer.concat(chunks),'gb2312');
let $ = cheerio.load(html, {decodeEntities: false});
// console.log($);
$('.co_content8 .ulink').each(function(i,d) {
let $d = $(d);
let titleHref = [];
titleHref.push({
href: $d.attr('href')
});
getLink(titleHref)
});
// console.log(ans);
});
});
}
// /*
//获取种子链接
function getLink(titleHref) {
console.log('进入getLink');
console.log(titleHref);
if(titleHref) {
titleHref.forEach(function(v,k) {
console.log('~~~~~~~~~~~~~~~~~~~~');
let infoUrl = Host + v.href;
// console.log(infoUrl);
http.get(infoUrl,function(res) {
const { statusCode } = res;
const contentType = res.headers['content-type'];
let error;
if (statusCode !== 200) {
error = new Error('请求失败。\n' +
`状态码: ${statusCode}`);
}
if (error) {
console.error(error.message);
// 消耗响应数据以释放内存
res.resume();
return;
}
console.log('进入getlink http');
let chunks = [];
res.on('data',function(chunk) {
chunks.push(chunk);
});
res.on('end', function(){
try {
let html = iconv.decode(Buffer.concat(chunks),'gb2312');
let $ = cheerio.load(html, {decodeEntities: false});
let bt = '';
bt = $('#Zoom td').children('a').attr('href');
// console.log(bt);
// console.log(typeof bt)
ans.push(bt);
}catch (e) {
console.error('bt',e.message);
}
})
}).on('error', (e) => {
console.error(`错误: ${e.message}`);
});
});
}
};
// */
for(let i = 1; i <= totalPage; i++) {
getTitleHref(baseUrl,i);
};
3 回复
解决了,自问自答吧,看来cnnode的都是高手,不屑于回答这么低级的问题😂
const cheerio = require('cheerio')
const http = require('http')
const iconv = require('iconv-lite')
const baseUrl = 'http://www.ygdy8.net/html/gndy/dyzz/list_23_'
const Host = 'http://www.ygdy8.net/'
const totalPage = 2 //指定爬多少页数据
let ans = []
//获取页面电影数据
function getTitleHref(url, page) {
return new Promise((resolve, reject) => {
let startUrl = url + page + '.html'
http.get(startUrl, function(res) {
const { statusCode } = res
let chunks = []
res.on('data', function(chunk) {
chunks.push(chunk)
})
res.on('end', function() {
let title = []
let html = iconv.decode(Buffer.concat(chunks), 'gb2312')
let $ = cheerio.load(html, { decodeEntities: false })
let titleHref = []
$('.co_content8 .ulink').each(function(i, d) {
let $d = $(d)
titleHref.push({
href: $d.attr('href')
})
})
resolve(getLink(titleHref))
})
})
})
}
// /*
//获取种子链接
function getLink(titleHref, cb) {
console.log('进入getLink')
console.log(titleHref)
if (titleHref) {
return Promise.all(
titleHref.map(function(v, k) {
return new Promise((resolve, reject) => {
console.log('~~~~~~~~~~~~~~~~~~~~')
let infoUrl = Host + v.href
http
.get(infoUrl, function(res) {
const { statusCode } = res
const contentType = res.headers['content-type']
let error
if (statusCode !== 200) {
error = new Error('请求失败。\n' + `状态码: ${statusCode}`)
}
if (error) {
console.error(error.message)
// 消耗响应数据以释放内存
res.resume()
return
}
let chunks = []
res.on('data', function(chunk) {
chunks.push(chunk)
})
res.on('end', function() {
try {
let html = iconv.decode(Buffer.concat(chunks), 'gb2312')
let $ = cheerio.load(html, { decodeEntities: false })
let bt = ''
bt = $('#Zoom td')
.children('a')
.attr('href')
resolve(bt)
} catch (e) {
reject(e)
}
})
})
.on('error', e => {
reject(e)
})
})
})
)
} else {
return Promise.resolve()
}
}
async function main() {
// */
let results = await Promise.all(
new Array(totalPage).fill().map((_, i) => getTitleHref(baseUrl, i + 1))
)
ans = ans.concat(...results)
console.log('get data:', ans)
}
main()
这个阶段学习node 异步强烈建议直接上 async/await, 理解也比较容易,代码也好读
慢慢来,坑总是要踩的