node-lessons
node-lessons copied to clipboard
Lesson4的挑战遇到问题
在抓取用户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]);
});
});
});
atoHref is undefined
here is corrected code:
atoHref = $('.reply_author').eq(0).attr('href');
你好,造成这个问题的原因可能是因为当你爬取的数量过多或者是帖子下面没有回复的时候,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)才行