利用urllib2和cookielib模块查询成绩~
懒是人类进步的阶梯!从此查成绩不用手!
思路
- 用cookielib创建一个cookie的容器
- 用urllib2.HTTPCookieProcessor(cookie)创建一个cookie处理器
- 用urllib2.build_opener(processor)创建打开的类(至此,从网站上获取的cookie也一并提交给学校了~)
- 模拟登录操作,获取cookies
- 打开成绩页面,观察url中的参数
- 指定需要查询的成绩
代码
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
| #-*- coding:utf-8 -*- import urllib2 import cookielib import re import string cookie = cookielib.CookieJar() handler = urllib2.HTTPCookieProcessor(cookie) opener = urllib2.build_opener(handler) def login(us,ps): req = urllib2.Request('http://jwgl.ahnu.edu.cn/login/check.shtml',data = 'user=%s&pass=%s&usertype=stu'%(us,ps)) req.add_header('User-Agent','Mozilla/5.0 (Windows NT 6.1; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0') content = opener.open(req).read() if 'fail' in content: print u'登录失败...' return False else: print u'登录成功...' return True def getlist(y,st): if y == '0': year = '0000-0000' semester = st else: yearadd1 = int(y)+1 year = y+'-'+str(yearadd1) semester = st req2 = urllib2.Request('http://jwgl.ahnu.edu.cn/query/cjquery/index?action=ok&xkxn=%s&xkxq=%s'%(year,semester)) req2.add_header('Referer','http://jwgl.ahnu.edu.cn/') content = opener.open(req2).read().decode('utf-8') if u'平时成绩公示' in content: reg = re.compile('<td align="left">(.+?)</td>\s*<td>.*?</td>\s*<td>.*?</td>\s*<td>(.*?)</td>\s*<td>.*?</td>\s*<td>.*?</td>\s*<td>.*?</td>\s*<td>(.*?)</td>\s*<td>.*?</td>\s*<td>.*?</td>\s*</tr>') else: reg = re.compile('<td align="left">(.+?)</td>\s*<td>.*?</td>\s*<td>.*?</td>\s*<td>(.*?)</td>\s*<td>.*?</td>\s*<td>.*?</td>\s*<td>(.*?)</td>\s*<td>.*?</td>\s*<td>.*?</td>\s*</tr>') chengji = re.findall(reg,content) if len(chengji)==0: print u'并未查询到..请检查参数..' class_name = [] credit = [] grade = [] grade2 = [] for i in chengji: class_name.append(i[0]) credit.append(i[1]) grade.append(i[2]) for i in grade: if i =='': i = u'暂无成绩' grade2.append(i) else: grade2.append(i) return [class_name, grade2, credit] def display(result): name = result[0] grade = result[1] credit = result[2] for i in range(len(name)): str1 = u'课程名称:%s' % (name[i]) str2 = u'成绩:%s'% (grade[i]) str3 = u'学分:%s\n'% (credit[i]) print str1 print str2 print str3 u = raw_input(u'输入用户:') p = raw_input(u'输入密码:') if login(u,p) is True: y = raw_input(u'输入要查询的年份(例如:0为全部学年.....2014为2014-2015年...):') st = raw_input(u'输入学期(0为全学期-1为上学期-2为下学期)') print u'正在努力查询中...' result = getlist(y,st) display(result) else: print u'你输入的用户名和密码有错..'
|
一些注意事项
- 成绩查询页面会判断Referer(你从哪个页面跳转过来的)
- 没了..
利用urllib2和cookielib模块查询成绩~
2017/01/18/get-grade/