[Python][ACM] 检测各大OJ刷题量的爬虫

寒假集训的时候,由于要统计做题量,所以到各大OJ来查阅做题量,我们可以使用Python来爬取题量,Codeforce的模块做的不是很成熟,可以调用CF的API来解决这个问题

readme:
1.自动获取POJ,HDU,ZOJ,FZU,ACdream,BZOJ上面的刷题量并且加以求和
2.自动获取Codeforce上面的积分
3.双线程更新Codeforce上的内容

说明:
1.直接输入账号就可以查看了
2.由于本弱还未想到如何去结束循环(万一有的用户名就那么奇葩呢),所以大家直接点右上角就可以了
3.本脚本的Python代码附在acm_crawler.py中,有什么建议,跪求告诉本弱

注意:
1.请务必在有网的时候使用本脚本,若网速极慢或者GFW问题的话或者账户名不存在,POJ,HDU,ZOJ,CodeForce,FZU,ACdream,BZOJ将返回0的结果,Codeforce将返回Not Found.
2.本脚本不会窃取您的账户信息,也不会对您的计算机造成主观上的伤害(如果您是老爷机并且什么脚本都运行不了的情况下,请您三思而后行)

将要更新:
1.UVA,UVAlive,lightOJ的支持

其他说明:
1.此脚本原理是爬虫的原理,要是出现了BUG请反馈本弱。
2.由于SGU,LightOJ,Ural,Uva均使用userid作为索引,所以无法查到对应网页,故无法予以支持。
3.由于华中科技大学的virtual judge交题量巨大,在连接Codeforce的时候往往要刷新多次窗口,故请在网速较快的时候使用。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
__author__ = 'kido'
import urllib2
import cookielib
import re
import thread
import time
import threading
import urllib
def detect_poj(name):
headers = {
'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6',
}
req = urllib2.Request(
url = 'http://poj.org/userstatus?user_id='+name,
headers = headers
)
cookie = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))
result = opener.open(req)
html =result.read()
#print html
sem = re.findall('Solved:</td>.*?<td align=center width=25%>.*?<.*?>(.*?)</a></td>',html,re.S)
#print re.search('Solved:</td>.*?<td align=center width=25%>.+<.*?>(.*?)</a></td>',html,re.S)
try :
return sem[0]
except:
return 0
#for item in sem:
#print item
#return item
#print result.read()
def detect_hdu(name):
headers = {
'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6',
}
req = urllib2.Request(
url = 'http://acm.hdu.edu.cn/userstatus.php?user='+name,
headers = headers
)
cookie = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))
result = opener.open(req)
html =result.read()
#print html
sem = re.findall('<tr><td>Problems Solved</td><td align=center>(.*?)</td></tr>',html,re.S)
try :
return sem[0]
except:
return 0
#for item in sem:
#print item
#return item
def detect_cf(name):
headers = {
'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6',
}
req = urllib2.Request(
url = 'http://codeforces.com/profile/'+name,
headers = headers
)
cookie = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))
result = opener.open(req)
html =result.read()
#print html
sem = re.findall('Contest rating:.*?<span.*?>(.*?)</span>',html,re.S)
#for item in sem:
#print item
#return item
try :
return sem[0]
except:
return 'Not found'
def detect_zoj(name):
headers = {
'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6',
}
req = urllib2.Request(
url = 'http://acm.zju.edu.cn/onlinejudge/showUserStatus.do?handle='+name,
headers = headers
)
cookie = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))
result = opener.open(req)
html =result.read()
#print html
sem = re.findall('<font size="3">AC Ratio:</font> <font color="red" size="4">(.*?)/.*?</font><br/>',html,re.S)
try:
return sem[0]
except:
return 0
#for item in sem:
#print item
#return item
#return 0
def detect_fzu(name):
headers = {
'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6',
}
req = urllib2.Request(
url = 'http://acm.fzu.edu.cn/user.php?uname='+name,
headers = headers
)
cookie = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))
result = opener.open(req)
html =result.read()
#print html
sem = re.findall('<tr>.*?<td>Total Accepted</td>.*?<td>(.*?)</td>.*?</tr>',html,re.S)
try:
return sem[0]
except:
return 0
def codeforce(start,end):
i = start
while i<end:
global cnt
i+=1
print '| detecting Codeforce page :',i,'/',lastpage
req = urllib2.Request(
url = 'http://codeforces.com/submissions/'+name+'/page/'+str(i)
)
#headers = headers
cookie = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))
result = opener.open(req)
html =result.read()
sem = re.findall('<span class=.verdict-accepted.>Accepted</span>',html,re.S)
cnt +=len(sem)
def detect_codeforce(name):
global cnt
cnt=0
headers = {
'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6',
}
req = urllib2.Request(
url = 'http://codeforces.com/submissions/'+name+'/page/1',
#headers = headers
)
cookie = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))
result = opener.open(req)
html =result.read()
#print html
sem = re.findall('<span class=.verdict-accepted.>Accepted</span>',html,re.S)
page = re.findall('<span class=.*? pageIndex=.*?><a href="/submissions/.*?/page/.*?">(.*?)</a></span>',html,re.S)
global lastpage
try :
#print 'last page : ',int(page[len(page)-1])
lastpage = int(page[len(page)-1])
except :
lastpage = 1
i=1
#print page
cnt=cnt+len(sem)
if lastpage > 10:
t = threading.Thread(target=codeforce,args=(1,lastpage/2))
p = threading.Thread(target=codeforce,args=(lastpage/2,lastpage))
threads =[]
threads.append(t)
threads.append(p)
for i in threads:
i.setDaemon(True)
i.start()
t.join()
p.join()
return cnt
def detect_acdream(name):
headers = {
'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6',
}
req = urllib2.Request(
url = 'http://acdream.info/user/'+name,
headers = headers
)
cookie = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))
try :
result = opener.open(req)
except :
return 0
html =result.read()
sem = re.findall('Submissions:.*?<span.*?>(.*?)</span>',html,re.S)
try:
return sem[0]
except:
return 0
def detect_dashiye(name):
headers = {
'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6',
}
req = urllib2.Request(
url = 'http://www.lydsy.com/JudgeOnline/userinfo.php?user='+name,
headers = headers
)
cookie = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))
result = opener.open(req)
html =result.read()
sem = re.findall(r'<tr bgcolor=#D7EBFF><td>Solved<td align=center><a href=.*?>(.*?)</a>',html,re.S)
try:
return sem[0]
except:
return 0
def detect_codef(name):
headers = {
'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6',
}
postdata = urllib.urlencode({
'handle':'kidozh',
'password':'(Protected)',
'action':'enter'
})
req = urllib2.Request(
url = 'http://codeforces.com/enter',
data=postdata,
headers = headers
)
cookie = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))
result = opener.open(req)
print '> Logined (by means of kidozh)'
#print result.read()
post = urllib.urlencode({
'isAdd':'true'
})
requ = urllib2.Request(
url ='http://codeforces.com/profile/'+name,
data = post,
headers = headers
)
result = opener.open(requ)
print '> Successfully attach ! '
#print result.read()
#file_object = open('attach.html','w')
#file_object.writelines(result.read())
#file_object.close()
#print result.read()
requ = urllib2.Request(
url = 'http://codeforces.com/problemset/standings',
data = post,
headers = headers
)
result = opener.open(requ)
#print result.read()
print '> Connected With dashboard'
html = result.read()
#file_object = open('dashboard.html','w')
#file_object.writelines(html)
#file_object.close()
#csrf = re.findall('<span style=.*? class=.*? data-csrf=.(\w*?).>&nbsp;</span>',html,re.S)
#code = csrf[0]
requ = urllib2.Request(
url = 'http://codeforces.com/problemset/standings?&friendsEnabled=on',
data = post,
headers = headers
)
result = opener.open(requ)
print '> Crawling Dashboard...'
#file_object = open('thefile.html','w')
html = result.read()
#print html
#file_object.writelines(html)
#file_object.close()
#match = name+'</a>.*?</td>.*?<td >.*?(\d*?).*?</td>'
match = name+'</a>(.*?)<tr>'
#p = re.compile(match,re.DOTALL)
#pi = p.findall(html)
#print re.search(match,html,re.DOTALL)
#print match[0]
mark = re.findall(match,html,re.S)
#print mark[0]
try :
num = re.findall('<td >.*?([0-9]+).*?<.td>.*?',mark[0],re.S)
except :
return 0
#print mark
nat = re.findall('.*?([0-9]+).*?',mark[0],re.S|re.DOTALL)
#print mark,num,nat
try :
return nat[0]
except :
return 0
#-------------main part---------------------
formattime = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
try:
print '-------------------------------'
print ' Author : kidozh '
print ' supported OJ : POJ,HDU,ZOJ,Codeforce,FZU,ACdream,BZOJ '
print ' Current time : '+formattime
print ' language : Python'
print ' if your accounts don\'t keep consistency you can type many times'
print '--------------------------------'
except :
print 'Cannot show welcome page...'+formattime
quit = 1
while 1:
name = raw_input('please input your account(For example : vjudge1): ')
poj = detect_poj(name)
print 'POJ : ',poj
hdu = detect_hdu(name)
print 'HDU : ',hdu
zoj = detect_zoj(name)
print 'ZOJ : ',zoj
fzu = detect_fzu(name)
print 'FZU : ',fzu
acdream = detect_acdream(name)
print 'ACdream : ',acdream
dashiye = detect_dashiye(name)
print 'BZOJ : ',dashiye
cf = detect_codef(name)
print 'CodeForce : ',cf
print 'Total : ',int(poj)+int(hdu)+int(zoj)+int(fzu)+int(acdream)+int(dashiye)+int(cf)
print '+----------------------Code Force Score----------------------'
print '+ Codeforce Score : ',detect_cf(name)