alsotang / node-lessons

:closed_book:《Node.js 包教不包会》 by alsotang
16.54k stars 4.7k forks source link

Lesson4的挑战遇到问题 #121

Open powerdmy opened 7 years ago

powerdmy commented 7 years ago

在抓取用户score时,合并用户的url遇到错误

//以上省略
var url = require('url');
var cnodeUrl = 'https://cnodejs.org/';

    topicUrls.forEach(function (topicUrl) {
      superagent.get(topicUrl)
        .end(function (err, res) {
          console.log('fetch ' + topicUrl + ' successful');
          var score = 0,
              $ = cheerio.load(res.text),
              atoHref = $('.reply_author').attr('href'),//得到用户的链接
              ato_href = url.resolve(cnodeUrl,atoHref);//这里报错
           //显示TypeError: Parameter "url" must be a string, not undefined
           //对用户页面进行请求
          superagent.get(ato_href)
            .end(function(err,rese){
                 console.log('fetch ' + ato_href + ' successful');
                    if(err){
                         console.error(err);
                    }
                  $ = cheerio.load(rese.text);
                  console.log(ato_href);
                  score = $('.user_profile').find('.big').html();
                  ep.emit('topic_html', [topicUrl, res.text,score]);
            });

        });
    });
Topppy commented 7 years ago

atoHref is undefined here is corrected code: atoHref = $('.reply_author').eq(0).attr('href');

zhx111 commented 7 years ago

你好,造成这个问题的原因可能是因为当你爬取的数量过多或者是帖子下面没有回复的时候,atoHref=$('.reply_author').eq(0).attr('href')的值会为undefined,造成TypeError: Parameter "url" must be a string, not undefined错误
所以需要先验证if ($('.reply_author').eq(0).attr("href")!=undefined)才行