利用python爬虫抓取OJ上做题信息(终结版) - Go语言中文社区

利用python爬虫抓取OJ上做题信息(终结版)


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'<td>Solved<td align=center><a href=.*?>(.*?)</a>'
    imgre_cug = re.compile(reg_cug)
    reg_hdu = r'<tr><td>Problems Solved</td><td align=center>(.*?)</td></tr>'
    imgre_hdu = re.compile(reg_hdu)
    reg_poj = '<tr><td width=15% align=left>Solved:</td>[sS]*?<td align=center width=25%><a href=.*?>(.+?)</a></td>'
    imgre_poj = re.compile(reg_poj)
    
    #将结果输出到html网页
    html = open('OJ.html', 'w')
    html.write("""
    <html>
     <head>
        <title>cug--hdu--poj统计</title>
        <style>img{float:left;margin:5px;}</style>
     </head>
    <body>
        """)
    
    html.write("""
    <center><table width=50%><tr><td colspan=3 align=left>
    </form></td><td colspan=3 align=right>
    </td></tr><tr class='toprow'>
    <td><b>Account</b>
    <td><b>cugOJ</b>
    <td><b>hdOj</b>
    <td><b>poj</b>
    <td><b>sum</b>
       """)   

    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("<p></p>")
        html.write("<tr>")
        html.write("<td>%s </td>" % lst[0] )
        html.write("<td>%s </td>" % str(lst[1]) )
        html.write("<td>%s </td>" % str(lst[2]) )
        html.write("<td>%s </td>" % str(lst[3]) )
        html.write("<td>%s </td>" % str(lst[4]) )
    html.write("</table></body><ml>")

    html.write('</body></html>')
    html.close()
    webbrowser.open_new_tab('OJ.html') #自动打开网页

result_hdu = "http://acm.hdu.edu.cn/userstatus.php?user="
result_cug = "http://acm.cug.edu.cn/JudgeOnline/userinfo.php?user="
result_poj = "http://poj.org/userstatus?user_id="

print "正在生成html网页......"
readFile(result_cug,result_hdu,result_poj)
print "html网页生成完毕,自动打开"



这个python爬虫程序的主要功能是爬取三个OJ(杭电,北大,地大)上的做题信息并进行题目的统计。

输入是昵称,其中有些人在几个OJ上的注册昵称都是一样的,有些不是,所以统计出来有些人在某个Oj上做题信息为0.

要读取的文件的信息如下图:



运行效果:


版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/bobopeng/article/details/18234667
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-03-01 22:37:14
  • 阅读 ( 1445 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢