统计hihoCoder挑战赛成绩的Python脚本

本文介绍了一个用于计算多次比赛总成绩排名的Python脚本。该脚本使用BeautifulSoup抓取网页上的比赛数据,并通过定制化的算法计算出参赛者的总分及排名,帮助作者及时了解自己在竞赛中的位置。

2月有两场比赛,总分前八的有纪念品。第一场排第11,要拿到奖品毫无把握。。心血来潮写了个用来计算几场比赛总分排名的脚本,好让自己第一时间知道能不能得奖(囧),也稍微试下BeautifulSoup。

# -*- coding: utf-8 -*-
from bs4 import BeautifulSoup
import urllib
def updateScoreList(scoreList, url):
    """
    :param scoreList: 
    :param url: 
    :return:
    """
    page = urllib.urlopen(url)
    soup = BeautifulSoup(page)
    contestants = soup.find_all(class_="fn-ell")
    for person in contestants:
        userName = person.string
        row = person.parent.parent # table row for this contestant
        stats = row.find_all('td')
        scoreTag = stats[2]
        score = int(unicode(scoreTag.string))
        if userName in scoreList:
            scoreList[userName]['score'] += score
            scoreList[userName]['numOfContests'] += 1
        else:
            scoreList[userName] = {'score':score,'numOfContests':1}

def generateRankList(urlList, numOfWinners):
    """
    :param urlList: url of hihocoder challenge contest rank pages
    :param numOfWinners: number of winners
    :return: the first numOfWinners winners in these contests
    """
    scoreList = {}
    for url in urlList:
        updateScoreList(scoreList, url)
    rank = []
    for userName, person in scoreList.items():
        rank.append((userName, person['score'], person['numOfContests']))
    rank.sort(cmp=lambda p1, p2:p2[1]-p1[1])
    return rank[:numOfWinners]

def main():
    urlList = []
    for num in range(8, 9):
        urlList.append("http://hihocoder.com/contest/challenge%d/rank" % num)
    rankList = generateRankList(urlList, 8)
    print u"用户名 总分 参赛次数"
    for person in rankList:
       print person

if __name__ == "__main__":
    main()
View Code

转载于:https://www.cnblogs.com/demoZ/p/4285124.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值