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

Lessons4 中挑战部分获取积分疑问

Open wadezha opened this issue 9 years ago • 9 comments

Lessons4 中挑战部分获取积分 var $ = cheerio.load(topicHtml); var authorUrl = $('.reply_author').eq(0).attr('href'); console.log('author url: ' + authorUrl); console.log('author: ' + $('.reply_author').eq(0).text().trim());

var score1 = ''; if(authorUrl){ var authorUrl = url.resolve(cnodeUrl, authorUrl); superagent.get(authorUrl) .end(function(err, res){ var $ = cheerio.load(res.text); score1 = $('.user_profile .big').eq(0).text(); console.log(score1); }); } return ({ title: $('.topic_full_title').text(), href: topicUrl, comment1: $('.reply_content').eq(0).text().trim(), author1: $('.reply_author').eq(0).text().trim(), score1: score1 }); 我用superagent去获取,但它是异步,导致最后返回值中score1是个空串,是否给个提示?是不是要使用同步的方式去获取这个积分?

wadezha avatar Feb 05 '15 11:02 wadezha

部分url请求的结果可能会给一个503 httpcode,猜测可能是因为cnode的服务器负载不行或者是服务器做了频率限制,不能保证每次请求的结果都是httpcode 200。

所以你跑这个demo的时候,部分url爬出来的html代码其实是一个503的警告页面,此时当然拿不到标题、分数之类的东西。

gejiawen avatar Apr 02 '15 18:04 gejiawen

稍微改进下,

var requestAgain = function (url, cb) {
    request.get(url).end(function (err, res) {
        if (err || res.statusCode !== 200) {
            requestAgain(url, cb);
            return;
        }

        cb(res);
    });
};

topicUrls.forEach(function (topicUrl) {

    requestAgain(topicUrl, function (res) {
        console.log('fetch ' + topicUrl + ' ' + res.statusCode);
        ep.emit('get_topic', {
            url: topicUrl,
            res: res
        });
    });

});

这样就保证了每次异步请求爬出来的html代码都是可用的。

gejiawen avatar Apr 02 '15 18:04 gejiawen

cnode 现在是有 ratelimit 的,不能爬得太暴力

2015-04-03 2:21 GMT+08:00 icake [email protected]:

稍微改进下,

var requestAgain = function (url, cb) { request.get(url).end(function (err, res) { if (err || res.statusCode !== 200) { requestAgain(url, cb); return; }

    cb(res);
});

};

topicUrls.forEach(function (topicUrl) {

requestAgain(topicUrl, function (res) {
    console.log('fetch ' + topicUrl + ' ' + res.statusCode);
    ep.emit('get_topic', {
        url: topicUrl,
        res: res
    });
});

});

— Reply to this email directly or view it on GitHub https://github.com/alsotang/node-lessons/issues/29#issuecomment-88999254 .

alsotang avatar Apr 03 '15 02:04 alsotang

Not Found. Resolve Domain Error. 是ratelimit的原因么

walter211 avatar Aug 12 '15 07:08 walter211

备案中。未登录用户无法进入。

2015-08-12 15:59 GMT+08:00 walter211 [email protected]:

Not Found. Resolve Domain Error. 是ratelimit的原因么

— Reply to this email directly or view it on GitHub https://github.com/alsotang/node-lessons/issues/29#issuecomment-130209934 .

alsotang avatar Aug 12 '15 08:08 alsotang

照猫画虎的爬到了author和score,但是并没有完全拼接在一起.

ep.after('topic_info',topicInfos.length,function(infos){
            INFOS = infos.map(function(infoHtml){
                var $ = cheerio.load(infoHtml);
                return({
                    anthor:$('.userinfo>.dark').text().trim(),
                    score:$('.user_profile .big').text().trim()
                });
                console.log(INFOS);
            });
        });
topicInfos.forEach(function(topicInfo){
            superagent.get(topicInfo)
                .end(function(err,res){
                    ep.emit('topic_info',res.text);
                });
        });

下面是我为了拼接写的一个东西,但是score又访问不到,这个该怎么办?

ep.after('topic_html',topicUrls.length,function(topics){
            TOPICS = topics.map(function(topicPair){
                var topicUrl  = topicPair[0];
                var topicHtml = topicPair[1];
                var $         = cheerio.load(topicHtml);
                var authorUrl = cnodeUrl+$('.topic_header>.changes>span:nth-child(2)>a').attr('href');
                var title     = $('.topic_full_title').text().trim();
                var href      = topicUrl;
                var comment   = $('.reply_content').eq(0).text().trim();
                var author    = $('.topic_header>.changes>span:nth-child(2)>a').text()
                var score     = superagent.get(authorUrl)
                    .end(function(err,res){
                        if(!res){
                            var tempScore = '0';
                        }else {
                            $ = cheerio.load(res.text);
                            var tempScore = $('.user_profile .big').text().trim();
                        }
                        return tempScore;
                    });
                return({
                    title:title,
                    href:href,
                    comment:comment,
                    author:author,
                    score:score
                });
            });
            console.log(TOPICS);
        });

JackieLs avatar Nov 29 '15 10:11 JackieLs

我想我犯了一个低级错误……因为是响应式布局我开了侧边栏的chrome把屏幕挤小了,然后没有看到user_card板块,所以导致我又去另一个网页爬了……forgive me...

JackieLs avatar Nov 29 '15 11:11 JackieLs

chrome 的侧边栏是啥?截图看看

在 2015年11月29日 下午7:39,JackieLs [email protected]写道:

我想我犯了一个低级错误……因为是响应式布局我开了侧边栏的chrome把屏幕挤小了,然后没有看到user_card板块,所以导致我又去另一个网页爬了……forgive me...

— Reply to this email directly or view it on GitHub https://github.com/alsotang/node-lessons/issues/29#issuecomment-160406263 .

alsotang avatar Nov 30 '15 03:11 alsotang

The Console....因为是响应式的....@media screen然后就被隐藏了么,我就以为这个页面没有记录author和score的地方。。。导致我又异想天开的去另一个地方爬,又不知道怎么拼接到一起~~~~不过走了一段弯路至少让我巩固了superagent,eventproxy,cheerio的用法

JackieLs avatar Dec 01 '15 02:12 JackieLs