Skip to content

Commit 945fc15

Browse files
committed
Add hub auto login
1 parent 24630fc commit 945fc15

File tree

6 files changed

+398
-335
lines changed

6 files changed

+398
-335
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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()
1.46 KB
Loading

crawler_snippet/hub_auto_login/loginhub.py

+17-11
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from StringIO import StringIO
77

88
username = 'U201110090'
9+
password = 'TEtZczQ2OTAxMDI='
910

1011

1112
s = requests.Session()
@@ -22,7 +23,7 @@
2223
image_to_string = None
2324
def get_rand_key():
2425
verify_url = 'http://hub.hust.edu.cn/randomKey.action?username=%s&time=%d' % (username, TIME)
25-
content = s.get(verify_url).content
26+
content = s.get(verify_url, headers={'referer': 'http://hub.hust.edu.cn/index.jsp'}).content
2627
global k1, k2
2728
k1, k2 = eval(content)
2829
verify_image_url = 'http://hub.hust.edu.cn/randomImage.action?k1=%s&k2=%s&uno=%s&time=%d' % (k1, k2, username, TIME)
@@ -35,7 +36,11 @@ def get_rand_key():
3536
try:
3637
from pytesser.pytesser import image_to_string
3738
vcode = image_to_string(vimg).strip()
39+
vimg.save('vcode.jpg')
40+
print vcode
3841
except:
42+
import traceback, sys
43+
traceback.print_exc(file=sys.stdout)
3944
vimg.show()
4045
vcode = raw_input('verify code:')
4146

@@ -55,20 +60,21 @@ def login():
5560
data['rand'] = vcode
5661
data['random_key1'] = k1
5762
data['random_key2'] = k2
63+
print vcode, k1, k2
5864
headers = {
59-
#'Host':'hub.hust.edu.cn',
60-
#'Origin':'http://hub.hust.edu.cn',
61-
#'Referer':'http://hub.hust.edu.cn/index.jsp',
62-
'User-Agent':
63-
'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.107 Safari/537.36',
64-
'Connection': 'keep-alive',
65+
'Host': 'hub.hust.edu.cn',
66+
'Origin': 'http://hub.hust.edu.cn',
67+
'Referer': 'http://hub.hust.edu.cn/index.jsp',
68+
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36'
69+
#'Connection': 'keep-alive',
6570
}
66-
res = s.post(url, data = data)
71+
# login post
72+
res = s.post(url, data = data, headers=headers)
73+
# print res.content
6774

68-
print res.content
6975
content = s.get('http://hub.hust.edu.cn/frames/body_left.jsp').content
70-
print content
71-
print s.cookies
76+
#print content
77+
#print s.cookies
7278

7379
if __name__ == '__main__':
7480
login()

0 commit comments

Comments
 (0)