Quantcast
Channel: CNode:Node.js专业中文社区
Viewing all articles
Browse latest Browse all 14821

写个小程序,获取cnode的前N个大神最幸运的那一个

$
0
0

无聊~~~ 写个小程序,获取cnode的前N个大神最幸运的那一个。闲话少说,上代码。高手勿喷😳

//抓取top n,并且随机一人获得奖励
const request = require('request');
const co = require('co');
const cheerio = require('cheerio');

//获取前N名
function getTop(topN){
    topN = topN > 0 ? topN : 10;
    topN = topN > 100 ? 100 : topN;
    const url = 'https://cnodejs.org/users/top100';
    return new Promise(function(resolve , reject){
        request.get(url , function(err,resp,body){
            if(err){
                resolve('error');
            }else{
                let $ = cheerio.load(body);
                let list = $('tbody tr .user_avatar');
                let ret = [];

                list.map(function(k,v){
                    ret.push(v.attribs.href.split('/').pop());
                });
                ret = ret.slice(0,topN);
                resolve(ret);
            }
        })
    })
}


co(function*(){
    let topNum = 13;
    let list = yield getTop(topNum);

    //随机获取一名
    let randNum = Math.floor(Math.random()*topNum);

    console.log("the lucker is :" , list[randNum] , " ta的排名: ", randNum+1);
})

快来看看你是不是那个人

喜欢小程序的可以加群: 451426451


Viewing all articles
Browse latest Browse all 14821

Trending Articles