码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-02-11 00:28:19      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:

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
        statics = row.find_all(td)
        scoreTag = statics[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

 

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

标签:

原文地址:http://www.cnblogs.com/demoZ/p/4285124.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!