-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathspider.py
185 lines (172 loc) · 6.99 KB
/
spider.py
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/usr/bin/env python
import requests
import re
import getpass
from time import sleep
import http.cookiejar
from PIL import Image
from bs4 import BeautifulSoup
from urllib import parse
from courseSchedule_Handler import courseSchedule_Handler
from score_Handler import score_Handler
from info_Handler import info_Handler
class Spider:
def __init__(self):
# create request headers
self.Url = 'http://xk4.ahu.cn'
agent = 'Mozilla/5.0 (X11; Linux x86_64; rv:54.0) Gecko/20100101 Firefox/54.0'
self.postUrl = self.Url + '/default2.aspx'
self.headers = {
'Referer': self.postUrl,
'User-Agent': agent
}
self.session = requests.session()
self.session.cookies = http.cookiejar.LWPCookieJar(filename='cookie')
def getCheckCode(self):
imgUrl = self.Url + '/CheckCode.aspx'
img = self.session.get(imgUrl)
with open('./checkCode.jpg', 'wb') as f:
f.write(img.content)
try:
code = self.killCheckCode()
except:
cc = Image.open('./checkCode.jpg')
cc.show()
code = input('please input the checkcode\n> ')
cc.close()
return code
def killCheckCode(self):
f1 = open('./kill_check_url', 'r')
kill_url = f1.read()
f1.close()
files = {'img': ('checkCode.jpg', open('./checkCode.jpg', 'rb'), 'image/png')}
res = requests.post(kill_url, files=files)
print(re.search(r"{{(....)}}", res.text).group(1))
return re.search(r"{{(....)}}", res.text).group(1)
def login(self, xh, password):
# create post data
self.xh = xh
self.password = password
RadioButtonList1 = u"学生".encode('gb2312', 'replace')
res = self.session.get(self.postUrl)
soup = BeautifulSoup(res.text, 'html.parser')
viewstate = soup.find('input', id='__VIEWSTATE').get('value')
eventvalidation = soup.find('input', id='__EVENTVALIDATION').get('value')
postData = {
'Button1': '',
'RadioButtonList1': RadioButtonList1,
'TextBox2': password,
'__EVENTVALIDATION': eventvalidation,
'__VIEWSTATE': viewstate,
'hidPdrs': '',
'hidsc': '',
'lbLanguage': '',
'txtSecretCode': self.getCheckCode(),
'txtUserName': xh
}
try:
loginPage = self.session.post(self.postUrl, data=postData, headers=self.headers)
self.session.cookies.save()
self.getInfo()
print('Login Successful')
except:
print('Login failed')
def getInfo(self):
infoUrl = self.Url + '/xsgrxx.aspx?xh=' + self.xh + '&'
self.session.headers['Referer'] = self.Url + '/xs_main.aspx?xh=' + self.xh
res = self.session.get(infoUrl)
handler = info_Handler(res.text)
self.name = handler.getInfo('name')
self.xm = parse.quote(self.name.encode('gb2312'))
def getCourseSchedule(self):
# get Course-Schedule
csUrl = self.Url + '/xskbcx.aspx?xh=' + self.xh + '&xm=' + self.xm + '&gnmkdm=N121603'
res = self.session.get(csUrl, headers=self.headers)
res.encoding = 'gb2312'
handler = courseSchedule_Handler(res.text)
handler.writeToFile('course.html')
print('CourseSchedule has saved as course.html')
def getScore(self):
# get Score
try:
scoreUrl = self.Url + '/xscjcx.aspx?xh=' + self.xh
self.session.headers['Referer'] = scoreUrl
res = self.session.get(scoreUrl)
soup = BeautifulSoup(res.text, 'html.parser')
viewstate = soup.find('input', id='__VIEWSTATE').get('value')
eventvalidation = soup.find('input', id='__EVENTVALIDATION').get('value')
postData = {
'__EVENTARGUMENT': '',
'__EVENTTARGET': '',
'__EVENTVALIDATION': eventvalidation,
'__VIEWSTATE': viewstate,
'btn_zcj': u'历年成绩'.encode('gb2312', 'replace'),
'ddlXN': '',
'ddlXQ': '',
'ddl_kcxz': '',
'hidLanguage': ''
}
except:
scoreUrl = self.Url + '/xscjcx_dq.aspx?xh=' + self.xh
self.session.headers['Referer'] = scoreUrl
res = self.session.get(scoreUrl)
soup = BeautifulSoup(res.text, 'html.parser')
viewstate = soup.find('input', id='__VIEWSTATE').get('value')
eventvalidation = soup.find('input', id='__EVENTVALIDATION').get('value')
postData = {
'__EVENTARGUMENT': '',
'__EVENTTARGET': '',
'__EVENTVALIDATION': eventvalidation,
'__LASTFOCUS': '',
'__VIEWSTATE': viewstate,
'btnCx': u' 查 询 '.encode('gb2312', 'replace'),
'ddlxn': u'全部'.encode('gb2312', 'replace'),
'ddlxq': u'全部'.encode('gb2312', 'replace')
}
res = self.session.post(scoreUrl, data=postData, headers=self.headers)
res.encoding = 'gb2312'
handler = score_Handler(res.text)
handler.writeToFile('score.html')
print("All the score has saved as score.html")
def getLesson(self, xkurl, id):
self.session.headers['Referer'] = self.Url + '/xsxk.aspx?xh=' + self.xh + '&xm=' + self.xm + '&gnmkdm=N121101'
html = self.session.get(xkurl)
soup = BeautifulSoup(html.text, 'html.parser')
viewstate = soup.find('input', id='__VIEWSTATE').get('value')
eventvalidation = soup.find('input', id='__EVENTVALIDATION').get('value')
xkkh = soup.find('table', class_='formlist').find_all('tr')[id].find_all('td')[-1].find('input').get('value')
postData = {
'RadioButtonList1': '1',
'__EVENTARGUMENT': '',
'__EVENTTARGET': 'Button1',
'__EVENTVALIDATION': eventvalidation,
'__VIEWSTATE': viewstate,
'xkkh': xkkh
}
while True:
sleep(1)
self.session.headers['Referer'] = xkurl
res = self.session.post(xkurl, data=postData)
pattern = re.compile(r".*alert\('([^']+)'\)")
ans = re.match(pattern, res.text)
print(ans.group(1))
if ans.group(1) == '保存成功!':
print("Congratulations!")
break
def main():
spider = Spider()
xh = input('please input xh\n>> ')
password = getpass.getpass('please input password\n>> ')
spider.login(xh, password)
# spider.getCourseSchedule()
# spider.getScore()
xkurl = input('please input xkurl (example: www.google.com)\n> ')
# 从上往下顺序
teacherid = input('please input teacherid (example: 1)\n> ')
while True:
try:
spider.getLesson(str(xkurl), eval(teacherid))
except:
spider.login(xh, password)
if __name__ == '__main__':
main()