试手登录了下我们学校的研究生信息管理系统,自动计算学分绩点
1 # -*- coding:utf-8 -*- 2 import urllib 3 import urllib2 4 import re 5 import cookielib 6 from bs4 import BeautifulSoup 7 8 class OUC: 9 def __init__(self,username,password): 10 self.loginUrl = "http://graduate.ouc.edu.cn/j_acegi_security_check" 11 # self.leftFrameUrl = "http://graduate.ouc.edu.cn/listLeft.do?" 12 self.frameGradeUrl = "http://graduate.ouc.edu.cn/accessModule.do?moduleId=25011&groupId=" 13 # self.gradeUrl = "http://graduate.ouc.edu.cn/listMyBulletin.do?" 14 self.cookies = cookielib.CookieJar() 15 self.postdata = urllib.urlencode({ 16 'j_username':username, 17 'j_password':password, 18 'groupId':'' 19 }) 20 self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cookies)) 21 22 def getPage(self): 23 user_agent = 'Mozilla/4.0(compatible;MSIE 5.5;Windows NT)' 24 headers = {'User-Agent':user_agent} 25 request = urllib2.Request( 26 url=self.loginUrl, 27 data=self.postdata, 28 headers=headers 29 ) 30 result = self.opener.open(request) 31 gradeResult = self.opener.open(self.frameGradeUrl) 32 btfsp = BeautifulSoup(gradeResult.read()) 33 # print btfsp.prettify() 34 35 content = btfsp.body.find_all("tr") 36 resultTable = [[0 for col in range(3)] for row in range(14)] 37 print u'专业名称'.rjust(15),' ',u'成绩',' ',u'学分' 38 i = 0 39 for item in content[1:]: 40 itemList = item.find_all("td") 41 classname = itemList[1].text.split(' ')[0].strip() 42 score = itemList[6].text.split(' ')[7].strip() 43 weight = itemList[7].text 44 resultTable[i][0] = classname 45 resultTable[i][1] = float(score) 46 resultTable[i][2] = float(weight) 47 print classname.rjust(15),' ',score.rjust(3),' ',weight.rjust(3) 48 i += 1 49 # print '%10s' % classname,'%-3s' % score.split(' ')[7],' ',weight 50 # 计算GPA 51 sumGPA = 0 52 sumWeight = 0 53 for k in range(14): 54 sumWeight += resultTable[k][2] 55 sumGPA += resultTable[k][1] * resultTable[k][2] 56 gpa = sumGPA / sumWeight 57 58 print u' 学分绩点为:',gpa 59 60 if __name__ == '__main__': 61 # ouc_stu = OUC() 62 print '================ Welcome ====================' 63 print u'Please enter your login messages:' 64 username = input('username:') 65 password = input('password:') 66 print u'Waiting...' 67 ouc_stu = OUC(username,password) 68 ouc_stu.getPage()
原文地址:http://www.cnblogs.com/leonwen/p/5730874.html