|
| 1 | +#!/usr/bin/env python2 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +import requests |
| 5 | +from PIL import Image |
| 6 | +from StringIO import StringIO |
| 7 | + |
| 8 | +username = 'U201110090' |
| 9 | +password = 'TEtZczQ2OTAxMDI=' |
| 10 | +#username = 'U201110087' |
| 11 | +#password = '158951'.encode('base64') |
| 12 | + |
| 13 | + |
| 14 | +s = requests.Session() |
| 15 | +content = s.get('http://bksjw.hust.edu.cn/index.jsp').content |
| 16 | +qstr = 'name="ln" value="' |
| 17 | +start = content.find(qstr) + len(qstr) |
| 18 | +server = content[start:start+len('app66.dc.hust.edu.cn')] |
| 19 | +k1 = 1 |
| 20 | +k2 = 1 |
| 21 | + |
| 22 | +import time |
| 23 | +TIME = int(time.time() * 1000) |
| 24 | +vimg = None |
| 25 | +image_to_string = None |
| 26 | +def get_rand_key(): |
| 27 | + verify_url = 'http://bksjw.hust.edu.cn/randomKey.action?username=%s&time=%d' % (username, TIME) |
| 28 | + content = s.get(verify_url, headers={'referer': 'http://bksjw.hust.edu.cn/index.jsp'}).content |
| 29 | + global k1, k2 |
| 30 | + k1, k2 = eval(content) |
| 31 | + verify_image_url = 'http://bksjw.hust.edu.cn/randomImage.action?k1=%s&k2=%s&uno=%s&time=%d' % (k1, k2, username, TIME) |
| 32 | + content = s.get(verify_image_url).content |
| 33 | + global vimg |
| 34 | + vimg = Image.open(StringIO(content)) |
| 35 | + |
| 36 | +get_rand_key() |
| 37 | + |
| 38 | +try: |
| 39 | + #from pytesser.pytesser import image_to_string |
| 40 | + #vcode = image_to_string(vimg).strip() |
| 41 | + import ocr |
| 42 | + vcode = ocr.get_img_num(vimg) |
| 43 | + vimg.save('vcode.jpg') |
| 44 | +except: |
| 45 | + import traceback, sys |
| 46 | + traceback.print_exc(file=sys.stdout) |
| 47 | + vimg.show() |
| 48 | + vcode = raw_input('verify code:') |
| 49 | + |
| 50 | +data = { |
| 51 | + 'usertype': 'xs', |
| 52 | + 'username': username, |
| 53 | + 'password': password, |
| 54 | + 'rand': '', |
| 55 | + 'ln': server, |
| 56 | + 'random_key1' : '', |
| 57 | + 'random_key2' : '', |
| 58 | + 'submit': '立即登录', |
| 59 | + } |
| 60 | + |
| 61 | +def login(): |
| 62 | + url = 'http://bksjw.hust.edu.cn/hublogin.action' |
| 63 | + data['rand'] = '%5d' % vcode |
| 64 | + data['random_key1'] = k1 |
| 65 | + data['random_key2'] = k2 |
| 66 | + headers = { |
| 67 | + 'Host': 'bksjw.hust.edu.cn', |
| 68 | + 'Origin': 'http://bksjw.hust.edu.cn', |
| 69 | + 'Referer': 'http://bksjw.hust.edu.cn/index.jsp', |
| 70 | + 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36' |
| 71 | + #'Connection': 'keep-alive', |
| 72 | + } |
| 73 | + # login post |
| 74 | + res = s.post(url, data = data, headers=headers) |
| 75 | + # print res.content |
| 76 | + |
| 77 | + content = s.get('http://bksjw.hust.edu.cn/frames/body_left.jsp').content |
| 78 | + #print content |
| 79 | + #print s.cookies |
| 80 | + |
| 81 | +def get_raw_score(): |
| 82 | + url = 'http://bksjw.hust.edu.cn/aam/score/QueryScoreByStudent_readyToQuery.action?cdbh=225' |
| 83 | + content = s.get(url, headers={'referer': 'http://bksjw.hust.edu.cn/frames/body_left.jsp'}).content |
| 84 | + key1str = ['name="key1" value="', '857702'] |
| 85 | + key2str = ['name="key2" value="', 'acde42e26e31da0e3b8e7093d9cf3685'] |
| 86 | + key1ind = content.find(key1str[0]) |
| 87 | + key2ind = content.find(key2str[0]) |
| 88 | + key1 = content[key1ind+len(key1str[0]):key1ind+len(key1str[0])+len(key1str[1])] |
| 89 | + key2 = content[key2ind+len(key2str[0]):key2ind+len(key2str[0])+len(key2str[1])] |
| 90 | + |
| 91 | + queryscoreurl = 'http://bksjw.hust.edu.cn/aam/score/QueryScoreByStudent_queryScore.action' |
| 92 | + data = { |
| 93 | + 'key1': key1, |
| 94 | + 'key2': key2, |
| 95 | + 'type': 'cj', |
| 96 | + 'stuSfid': username, |
| 97 | + 'xqselect':'20132' |
| 98 | + } |
| 99 | + headers={ |
| 100 | + 'Host' :'bksjw.hust.edu.cn', |
| 101 | + 'Origin': 'http://bksjw.hust.edu.cn', |
| 102 | + 'Referer': 'http://bksjw.hust.edu.cn/aam/score/QueryScoreByStudent_readyToQuery.action?cdbh=225' |
| 103 | + } |
| 104 | + content = s.post(queryscoreurl, data=data, headers=headers).content |
| 105 | + ind = content.find('cachedId=') |
| 106 | + cachedId = content[ind+len('cachedId=') : ind+content[ind:].find('&')] |
| 107 | + ind = content.find('reportParamsId=') |
| 108 | + reportParamsId = content[ind+len('reportParamsId=') : ind+content[ind:].find('&')] |
| 109 | + rawscoreurl = ('http://bksjw.hust.edu.cn:80/reportServlet?action=18&file=student_personal_score_grade.raq&srcType=file&separator=%09&reportParamsId=' + |
| 110 | + reportParamsId + '&cachedId=' + |
| 111 | + cachedId +'&t_i_m_e=' + str(TIME) ) |
| 112 | + content = s.get(rawscoreurl).content |
| 113 | + print content.decode('gb2312') |
| 114 | + |
| 115 | +if __name__ == '__main__': |
| 116 | + login() |
| 117 | + get_raw_score() |
0 commit comments