模擬登錄教務處獲取成績

剛開學時班長髮了每一個人的身份證號碼,被別有用心的boy存到了本地。教務處密碼是身份證後六位,而後我就悄悄地(偷偷地)製做了一張均分排名(⊙v⊙)。正則表達式

過程當中學習了模擬登錄以及正則表達式的基本應用。原本是用庫urllib的,結果遇到重定向302問題不會提取響應報文,而後就用了第三方庫requests(順便解決了編碼問題). 還加入了第三方庫xlrd用來讀取本地的excel.瀏覽器

代碼以下:cookie

  1 # -*- coding: utf-8 -*-
  2 import re
  3 import xlrd
  4 change_sum = 0 #用以存儲密碼修改的人數
  5 grade_course = ['*************'] 用以存儲計算均分的科目
  6 class course():
  7     '''the class of course'''
  8     def __init__(self,L):
  9         self.name = L[2] #學科名稱
 10         self.credit = L[3]  #學科學分
 11         self.grade = L[8] #學科分數
 12         self.rank = L[9] #學科排名
 13 class person():
 14     '''the class of person'''
 15     def __init__(self,name,S):
 16         self.name = name
 17         self.courses = S
 18         Grade = 0
 19         Credit = 0
 20         for key,value in S.items():
 21             global grade_course
 22             if key in grade_course:
 23                 try:
 24                     t = float(value.grade)
 25                     Credit += float(value.credit)
 26                     Grade += t*float(value.credit)
 27                 except:
 28                     pass
 29         self.average = Grade/Credit #均分
 30 def save(Path,Data):
 31     '''save Data in Path'''
 32     file_obj = open(Path,'a')
 33     try:
 34         file_obj.write(Data)
 35     finally:
 36         file_obj.close()
 37 def gettext(username,password):
 38     '''input username & password, return marks text'''
 39     import requests
 40     
 41     def getlt(data):
 42         '''獲取學校教務處hidden的參數lt'''
 43         cer = re.findall('(?<=name=\"lt\" value=\").+?(?=\")',data)
 44         return cer[0]
 45     def getexecution(data):
 46         '''獲取學校教務處hidden的參數execution'''
 47         cer = re.findall('(?<=name=\"execution\" value=\").+?(?=\")', data)
 48         return cer[0]
 49     
 50     header = { #假裝瀏覽器
 51         'Connection' : 'Keep-Alive',
 52         'Accept-Language' : 'zh-CN',
 53         'Accept' : 'image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*',
 54         'User-Agent' : 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.3; WOW64; Trident/7.0; .NET4.0E; .NET4.0C; .NET CLR 3.5.30729; .NET CLR 2.0.50727; .NET CLR 3.0.30729; InfoPath.2; Tablet PC 2.0; GWX:DOWNLOADED; GWX:RESERVED; GWX:QUALIFIED; SMJB)',
 55         'Accept-Encoding' : 'gzip, deflate',
 56         'Host' : '*********',
 57         'DNT': '1'
 58     }
 59 
 60     url = '**********' #學校教務處網址
 61     s = requests.session() #保存cookies
 62     r = s.get(url,headers=header)
 63 
 64     lt = getlt(r.text)
 65     execution = getexecution(r.text)
 66     payload = { #構造請求頭
 67         'username' : username,
 68         'password': password,
 69         'submit' : '',
 70         'lt' : lt,
 71         'execution' : execution,
 72         '_eventId' : 'submit',
 73         'rmShown' : '1'
 74     }
 75 
 76     login = s.post(url,data=payload,headers=header)
 77 
 78     if login.status_code == 200:
 79         print('%s login success!'%name)
 80         temp = s.get('**************',headers=header)
 81         preurl = '**************'
 82         dox = '************' #存放成績的地址
 83         newurl = preurl + dox
 84         temp = s.get(newurl,headers=header)
 85         return temp.text
 86     else:
 87         print('%s login failed, login.status_code:'%name+login.status_code)
 88         return ''
 89 def textanal(name,text):
 90     '''analysis the text of name, and save its grades'''
 91     if text :
 92         prepath = '***********' #保存我的成績的地址
 93         path = prepath + name +'.txt'
 94         
 95         #從網頁中獲取成績
 96         marks = text.split('''<tr class="odd" onMouseOut="this.className='even';" onMouseOver="this.className='evenfocus';">''')
 97         courses = {}
 98         n = 0
 99         for cours in marks:
100             if n > 0 :
101                 L = re.findall('(?<=\s)[^<|\s][^>]*?(?=\s)',cours)
102                 if len(L) >= 10 :
103                     while len(L) > 10 :
104                         del L[3]
105                     key = L[2]
106                     value = course(L)
107                     save(path,' '.join(L)+'\n')
108                     courses[key] = value
109                 elif len(L) != 10 :
110                     print('Error : wrong course')
111             n += 1
112         if courses :
113             one = person(name,courses)
114             path = prepath + 'allmarks.txt'
115             save(path,name+' '+str(one.average)+'\n') #記錄此人均分
116         else :
117             global change_sum
118             change_sum += 1
119             print('%s password changed or exist verification code!'%name)
120 
121 #讀取每一個人的username和password        
122 book = xlrd.open_workbook('************')
123 sheet = book.sheet_by_index(0)
124 nrows = sheet.nrows
125 for x in range(nrows):
126     row = sheet.row_values(x)
127     name = row[5]
128     username = row[2]
129     password = row[15][-6:]
130     text = gettext(username,password)
131     textanal(name,text)
132 print(change_sum) #輸出修改密碼的人的個數

最後用excel處理下數據就好啦!session

然而最後發現修改了密碼的人好少= =只有個位數app

相關文章
相關標籤/搜索