Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2020-02-24 实现一个抽奖程序 #15

Open
fanmingfei opened this issue Mar 24, 2020 · 2 comments
Open

2020-02-24 实现一个抽奖程序 #15

fanmingfei opened this issue Mar 24, 2020 · 2 comments

Comments

@fanmingfei
Copy link
Member

fanmingfei commented Mar 24, 2020

写一个爬虫,收集一下所有回复ISSUE做题的人,并且抽出一人中奖。
ps:不需要有UI,只需要有程序就好,不一定非要在浏览器中跑,node里面跑也可以。

@joker1148
Copy link

var request = require('request');
var cheerio = require('cheerio');

let num = 0
let resultArr = []
function filter(data){
    var $ = cheerio.load(data);
    var temp_arr = $('strong[class = "css-truncate"]').text();
    let res = temp_arr.replace(/\s+/g, ' ').split(" ")
    var result = res.filter(function (res) {
        return res && res.trim()
    });
    return result
}
function getName(){
    for(let i = 1;i<=15;i++){
        request(`https://github.com/SlashLabTeam/2021questions/issues/${i}`, function (error, response, body) {
            console.log(`====正在爬第${i}页====`)    
            if (!error && response.statusCode == 200) {
              let arr = filter(body)
              resultArr.push(...arr)
              console.log(resultArr)
              num++
              console.log("num:"+num)
            }
            if(num==15){
                setTimeout(()=>{
                    let len = resultArr.length
                    let win = Math.floor(Math.random()*len)
                    console.log("总人数:"+len)
                    console.log("赢家:"+resultArr[win])
                    return 
                },1000)     
            }
          })
    }
    return
}
getName()

@xiaosen7
Copy link

const axios = require('axios');
const BASE_URL = 'https://github.com/SlashLabTeam/2021questions/issues';
const reg1 = /issue\_(\d+)\_link/g
const reg2 = new RegExp("<a class=\"author link-gray-dark css-truncate-target width-fit\"[^>]+>([^<]+)</a>", 'g');

console.log("正在爬取网页");
axios.get(BASE_URL).then(({ data: html }) => {

    console.log("正在获取选手");

    const bucket = [], promisis = [], users = [];

    html.replace(reg1, (_, i) => {
        bucket.push(BASE_URL + `/${i}`)
    })

    bucket.forEach(url => {

        promisis.push(axios.get(url).then(({ data: html }) => {
            html.replace(reg2, (_, name) => {
                if (!users.includes(name))
                    users.push(name);
            });
        }));
    });

    Promise.all(promisis).then(res => {
        console.log(...users);

        console.log('正在抽取幸运选手');

        console.log(users[Math.ceil(Math.random() * users.length - 1)]);
    }, e => {
        console.log(e);
    });
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants