标签:
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()
标签:
原文地址:http://www.cnblogs.com/demoZ/p/4285124.html