Python爬取个人所有Codeforces比赛每个时间段内出题情况

版权声明:欢迎转载,请注明此博客地址。 https://blog.csdn.net/Ever_glow/article/details/85326701

工具:PyChram

分析:由个人页面可以得到个人最近的比赛的数据

检查元素发现可以利用Rank排名得到每个人在每场比赛中的排名,状态如下:

检查元素中,容易发现有特定id,并且时高亮显示,但千万不要利用高亮显示,高亮显示是后台渲染的,所以可以利用专属id,而且专有id可以在上个页面中爬取。

利用bs4得到所有id为专属项目的tr,然后得到每一个时间的td,切割出时间,直接字符串比较。把时间分成n个时间段,然后得到y坐标。最后输出(x,y)的坐标系。

代码实现:

import pylab
import requests
import numpy
import re
import xlwt
import urllib
import bs4
url = "http://codeforces.com/contests/with/passer__"
str1 = requests.get(url).text
aim = r'/contest/.*/standings/.*/.*'
a = re.compile(aim)
li = a.findall(str1)

#case 1 00:00-00:12
#case 2 00:13-00;43
#case 3 00:44-01;40
#case 4 01:41-01-50
#case 5 01:51-01:55
#case 6 01;56-
x = [1,2,3,4,5,6]
y = [0,0,0,0,0,0]

for i in li:
    i = i[:-1]
    i = i[:-2]
    ss_id = i[-18:-10]
    url = "http://codeforces.com"+i
    str2 = requests.get(url).text
    bs = bs4.BeautifulSoup(str2,'lxml')
    soul = bs.find_all('tr',participantid=ss_id)
    temp = str(soul)
    now = r'<span class="cell-time">(\d*:\d*)'
    aim_now = re.compile(now)
    li_now = aim_now.findall(temp)
    for j in li_now:
        if str(j) <= "00:12":
            y[0] += 1
        elif str(j) <= "00:43":
            y[1] += 1
        elif str(j) <= "01:41":
            y[2] += 1
        elif str(j) <= "01:50":
            y[3] += 1
        elif str(j) <= "01:55":
            y[4] += 1
        else:
            y[5] += 1

pylab.plot(x,y)
pylab.show()

结果如下:

这就是菜鸡选手的做题曲线,前1小时做做水题,最后运气好能憋出几个。哎。。。。。

猜你喜欢

转载自blog.csdn.net/Ever_glow/article/details/85326701