[python] HDU自动登录提交代码程序
调了一个下午,被python的正则绊住了;在C#上运作好好的式子在python老是报错,原来python的断言式必须是固定长度的,像类似(?<=[^>].*?)的零宽度正回顾后发断言是不允许出现的,这点python做的没有C# 方便,弄得我只好分了好几个步骤来做.
1 __author__ = 'wuminye' 2 import time 3 import urllib 4 import urllib2 5 import cookielib 6 import re 7 8 std = ['Queuing', 'Compiling', 'Running'] 9 10 hostaddr = 'http://acm.hdu.edu.cn' 11 loginaddr = '/userloginex.php?action=login' 12 submaddr = '/submit.php?action=submit' 13 statusaddr = '/status.php' 14 15 cj = cookielib.LWPCookieJar() 16 cookie_support = urllib2.HTTPCookieProcessor(cj) 17 #httpHandler = urllib2.HTTPHandler(debuglevel=1) 18 opener = urllib2.build_opener(cookie_support, urllib2.HTTPHandler) 19 urllib2.install_opener(opener) 20 header = {'User-Agent': 'IE10'} 21 22 23 def login(username, password): 24 """ 25 26 :rtype : object 27 """ 28 postData = {'username': username, 29 'userpass': password, 30 'submit': 'Sign In'} 31 postData = urllib.urlencode(postData) 32 request = urllib2.Request(hostaddr + loginaddr, postData, header) 33 response = urllib2.urlopen(request, timeout=10) 34 if response.getcode() != 200 and response.getcode() != 302: 35 print 'login web error!' 36 return False 37 ans = response.read() 38 if ans.find("Sign Out") == -1: 39 return False 40 return True 41 42 43 def querystatus(): 44 postData = {'user': 'wuminye', 45 'lang': 0, 46 'first': '', 47 'pid': '', 48 'status': 0} 49 postData = urllib.urlencode(postData) 50 request = urllib2.Request(hostaddr + statusaddr + '?' + postData, headers=header) 51 response = urllib2.urlopen(request, timeout=10) 52 if response.getcode() != 200 and response.getcode() != 302: 53 print 'query web error!' 54 return False 55 ans = response.read() 56 m = re.search('(?<=<td height=22px>).*?</td>(<td.*?</td>){8}(?=</tr>)', ans) 57 ans = '<td>' + m.group(0) 58 mylist = [] 59 for i in range(0, 7): 60 m = re.search(r"(?<=<td).+?(?=</td>)", ans) 61 t = m.group()[1:] 62 n = t.find('>') 63 if n != -1: 64 t = t[n + 1:] 65 t = re.search('[^<]+', t) 66 mylist.append(t.group()) 67 ans = ans[m.end() + 5:] 68 for i in range(0, 4): 69 ans = ans[ans.find('>') + 1:] 70 ans = re.search(r"[^<]+", ans).group() 71 mylist.append(ans) 72 return mylist 73 74 75 def subbmit(pid, lan, code): 76 postData = {'problemid': pid, 77 'language': lan, 78 'usercode': code, 79 'submit': 'Submit'} 80 postData = urllib.urlencode(postData) 81 request = urllib2.Request(hostaddr + submaddr, postData, header) 82 response = urllib2.urlopen(request, timeout=10) 83 if response.getcode() != 200 and response.getcode() != 302: 84 print 'subbmit web error!' 85 return False 86 print 'Subbmit Done!' 87 ans = querystatus() 88 while ans[2] in std: 89 time.sleep(1) 90 print 'Status:', ans[2] 91 ans = querystatus() 92 print ans