账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    node大佬请进!电影天堂的node爬虫,为什么没进入titleHref.forEach就崩了??
    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 条
    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部