node-lessons icon indicating copy to clipboard operation
node-lessons copied to clipboard

Lesson4的挑战遇到问题

Open powerdmy opened this issue 8 years ago • 2 comments

在抓取用户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]);
            });

	    });
	});

powerdmy avatar Jan 20 '17 06:01 powerdmy

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

Topppy avatar Jan 20 '17 13:01 Topppy

你好,造成这个问题的原因可能是因为当你爬取的数量过多或者是帖子下面没有回复的时候,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)才行

zhx111 avatar Jan 24 '17 07:01 zhx111