import webbrowser
import re
import urllib
#获取hdu网页
def getHtml_hdu(url):
page = urllib.urlopen(url)
html = page.read()
#unicodehtml = html.decode("utf-8")
#return unicodehtml
return html
#获取poj网页
def getHtml_poj(url):
page = urllib.urlopen(url)
html = page.read()
#unicodehtml = html.decode("utf-8")
#return unicodehtml
return html
#获取cug网页
def getHtml_cug(url):
page = urllib.urlopen(url)
html = page.read()
unicodehtml = html.decode("utf-8")
return unicodehtml
#获取hdu中用户信息
def zhenghe_hdu(str1,userid,imgre):
html=getHtml_hdu( str1+userid )
return re.findall(imgre,html)
#获取cug中用户信息
def zhenghe_cug(str1,userid,imgre):
html=getHtml_cug( str1+userid )
return re.findall(imgre,html)
#获取poj中用户信息
def zhenghe_poj(str1,userid,imgre):
html =getHtml_poj( str1+ userid)
return re.findall(imgre,html)
#文件读出用户账号进行统计
def readFile(result_cug,result_hdu,result_poj):
file_object = open("users.txt",'r')
reg_cug = r'Solved | (.*?)'
imgre_cug = re.compile(reg_cug)
reg_hdu = r' | Problems Solved | (.*?) |
'
imgre_hdu = re.compile(reg_hdu)
reg_poj = 'Solved: | [\s\S]*?(.+?) | '
imgre_poj = re.compile(reg_poj)
#将结果输出到html网页
html = open('OJ.html', 'w')
html.write("""
cug--hdu--poj统计
""")
html.write("""
|
|
Account
| cugOJ
| hdOj
| poj
| sum
""")
alist = [] #定义一个列表
for line in file_object:
line=line.strip('\n') #去掉读取的每行的"\n"
list_hdu = zhenghe_hdu(result_hdu,line,imgre_hdu)
list_cug = zhenghe_cug(result_cug,line,imgre_cug)
list_poj = zhenghe_poj(result_poj,line,imgre_poj)
if len(list_hdu) == 0:
number_hdu = 0
else:
number_hdu = eval(list_hdu[0])
if len(list_cug) == 0:
number_cug = 0
else:
number_cug = eval(list_cug[0])
if len(list_poj) == 0:
number_poj = 0
else:
number_poj = eval(list_poj[0])
alist.append([line,number_cug,number_hdu,number_poj,number_cug+number_hdu+number_poj])
print "处理完一个用户信息"
for i in range(len(alist)): #冒泡排序
for j in range(len(alist)):
if alist[i][4] > alist[j][4]:
tmp = alist[i]
alist[i] = alist[j]
alist[j] = tmp
for lst in alist: #输出到网页
html.write("")
html.write(" |
")
html.write("%s | " % lst[0] )
html.write("%s | " % str(lst[1]) )
html.write("%s | " % str(lst[2]) )
html.write("%s | " % str(lst[3]) )
html.write("%s | " % str(lst[4]) )
html.write("
")
html.write('