- 40
- 0
旧代码的问题是res.on('end',...)是异步的,与可能titleHref都还是空的。现在我更新了代码,解决了这个问题,但是怎么打印出结果的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);
};
--------------------------------------------------------分割线-------
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/";
let titleHref = [];
const totalPage = 1; //指定爬多少页数据
let res = [];
//获取页面电影数据
function getTitleHref(url,page) {
let startUrl = url+page+".html";
http.get(startUrl,function(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);
titleHref.push({
href: $d.attr('href')
});
});
console.log(titleHref);
});
if(page <= totalPage) {
getTitleHref(url,++page);
}else {
console.log(page);
getLink(titleHref);
}
});
}
//获取种子链接
function getLink(titleHref) {
console.log('进入getLink');
titleHref.forEach(function(v,k) {
console.log('~~~~~~~~~~~~~~~~~~~~');
let infoUrl = Host + v.href;
console.log(infoUrl);
// try {
http.get(infoUrl,function(res) {
console.log('进入getlink http');
let chunks = [];
res.on('data',function(chunk) {
chunks.push(chunk);
});
res.on('end', function(){
let html = iconv.decode(Buffer.concat(chunks),'gb2312');
let $ = cheerio.load(html, {decodeEntities: false});
let reg = /.*译 名/;
let info = '';
let bt = '';
let textInfo = $('.co_content8 #Zoom p').eq(0).text();
info = textInfo.match(reg)[0];
bt = $('#Zoom td').children('a').attr('href');
res.push({
Info:info,
Bt:bt
});
console.log(res);
})
//怎么捕获错误!!!
//res.on('error',function(){
// console.log('error');
//})
})
// }catch(e) {
// console.log(e);
// }
});
};
getTitleHref(baseUrl,1)
如上面代码,异步太多,不知道是哪里的问题,求node大神指教,getLink()函数里的titleHref.forEach都没进去到,node线程就崩了?console.log('进入getlink http')也是没有打印的
1
打赏
收藏
点击回答
您的回答被采纳后将获得:提问者悬赏的 11 元积分
网站公告
- 扫一扫访问手机版
回答动态

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器更新之后。服务器里面有部分玩家要重新创建角色是怎么回事啊?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题函数计算不同地域的是不能用内网吧?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题ARMS可以创建多个应用嘛?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题在ARMS如何申请加入公测呀?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题前端小程序接入这个arms具体是如何接入监控的,这个init方法在哪里进行添加?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器刚到期,是不是就不能再导出存档了呢?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器的游戏版本不兼容 尝试更新怎么解决?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器服务器升级以后 就链接不上了,怎么办?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器转移以后服务器进不去了,怎么解决?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器修改参数后游戏进入不了,是什么情况?预计能赚取 0积分收益
- 回到顶部
- 回到顶部
