This repository has been archived by the owner on Feb 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspider.py
377 lines (330 loc) · 14.4 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# -*- coding: utf-8 -*-
"""
Created on 2016.12.02
@author: 15999222
"""
import platform
import os
import re
import requests
from urllib.parse import quote
from bs4 import BeautifulSoup
import parserInfo
import func
from database import data_conn
import datetime
from PIL import Image
import json
import warnings
warnings.filterwarnings("ignore")
IFLOGIN = "请登录"
class Student:
def __init__(self,num = None,password = None,pic2Num = None):
self.st_num = num # 学号
self.st_password = password # 密码
self.st_name = None # 姓名
self.st_urlName = None # url编码后的姓名
self.session = requests.session()
self.baseUrl = "http://210.30.208.140/"
self.url2Num = pic2Num
self.avail_courses_year = []
self.avail_courses_term = []
self.avail_grade_year = []
self.avail_grade_term = []
self.__VIEWSTATE2 = None
self.__VIEWSTATE3 = None
def login(self):
# 访问教务系统
status = True
print("正在尝试登录......")
self.session.headers['User-Agent'] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'
response = self.session.get(self.baseUrl)
self.baseUrl = response.url
self.baseUrl = re.subn(r'.default2.aspx', '', self.baseUrl)[0]
loginUrl = self.baseUrl + '/default2.aspx'
while (status):
response = self.session.get(loginUrl)
__VIEWSTATE = re.findall("name=\"__VIEWSTATE\" value=\"(.*?)\"", response.content.decode('GBK'))[0]
# print(__VIEWSTATE)
# print("Got viewatate")
print( "正在获取验证码......" )
imgUrl = self.baseUrl + "/CheckCode.aspx?"
imgresponse = self.session.get(imgUrl, stream=True)
image = imgresponse.content
# 保存code
DstDir = None
if 'Linux' in platform.system():
DstDir = os.getcwd() + "/" + "code.jpeg"
# print(DstDir)
with open(DstDir, "wb") as jpg:
jpg.write(image)
print("保存验证码到:" + DstDir)
os.popen("display " + DstDir)
elif 'windows' in platform.system():
DstDir = os.getcwd() + "\\" + "code.jpeg"
# print(DstDir)
with open(DstDir, "wb") as jpg:
jpg.write(image)
print("保存验证码到:" + DstDir)
command = "start" + " \"\" " + DstDir
os.popen( command ).read()
elif 'Darwin' in platform.system():
DstDir = os.getcwd() + "/" + "code.jpeg"
# print(DstDir)
with open(DstDir, "wb") as jpg:
jpg.write(image)
print("保存验证码到:" + DstDir)
# os.popen("open " + DstDir)
# code = input("验证码是:")
im = Image.open(DstDir)
im = im.convert('RGB')
im.save(DstDir, 'jpeg')
# print(DstDir)
code = ''
with open('code.jpeg', 'rb') as codeFile:
postFile = {'attachment_file': ('code.jpeg', codeFile, 'image/png', {})}
r1 = requests.post(self.url2Num, files = postFile)
code = (json.loads(r1.text))['code']
print(code)
RadioButtonList1 = u"学生".encode('gb2312', 'replace')
data = {
"RadioButtonList1": RadioButtonList1,
"__VIEWSTATE": __VIEWSTATE,
"txtUserName": self.st_num,
"TextBox2": self.st_password,
"Button1": "",
"txtSecretCode": code,
}
Loginresponse = self.session.post(loginUrl, data=data)
print("尝试登录中......")
url2 = self.baseUrl + "/xs_main.aspx?xh=" + self.st_num
self.session.headers['Referer'] = self.baseUrl + "default2.aspx"
response2 = self.session.get(url2)
html = response2.content.decode("gb2312")
soup = BeautifulSoup(html, 'html.parser')
title = soup.title.get_text()
print(title)
if title.find(IFLOGIN) != -1: # To determine whether the login is successful
print("登录失败,正在重新登录......")
IFCONTINUE = input("输入 0 以结束")
if IFCONTINUE == '0':
break
else:
self.st_name = re.findall(r'<span id="xhxm">([\w\W]+)同学</span>',str(soup.find(id="xhxm")))[0]
self.ECname()
status = False
if not status:
print("成功登录教务系统")
ob_sql = data_conn()
ob_sql.start()
conn_new = ob_sql.conn
cursor = conn_new.cursor()
cursor.execute("select * from login_info WHERE stu_id = '%s'" % self.st_num)
now_time = datetime.date.today().isoformat()
if cursor.fetchone() is None:
cursor.execute("insert into login_info(stu_id,stu_passwd,login_time) \
VALUES ('%s','%s','%s')" % (self.st_num,self.st_password,now_time))
else:
cursor.execute("delete from login_info where stu_id = '%s' " % self.st_num)
cursor.execute("insert into login_info(stu_id,stu_passwd,login_time) \
VALUES ('%s','%s','%s')" % (self.st_num, self.st_password, now_time))
conn_new.commit()
ob_sql.end()
else:
print("登录教务系统终止!!!")
return 1
def ECname(self):
self.st_urlName = quote(self.st_name.encode('gb2312'))
return
def get_ess(self):
# 从课程便初始页面获取__VIEWSTATE 可选择的学年和学期可选项
url2 = self.baseUrl + "/xskbcx.aspx?xh=" + self.st_num + "&xm=" + self.st_urlName + "&gnmkdm=N121603"
self.session.headers[ 'Referer' ] = self.baseUrl + '/xs_main.aspx?xh=' + self.st_num
response2 = self.session.get(url2)
html = response2.content.decode("gb2312")
soup = BeautifulSoup( html, 'html.parser')
self.__VIEWSTATE2 = soup.findAll('input')[2]['value']
self.avail_courses_year = [item.get_text() for item in soup.find(id = 'xnd').find_all('option')]
self.avail_courses_term = [item.get_text() for item in soup.find(id = 'xqd').find_all('option')]
url_info_course_schedule = self.baseUrl + "/xsxkqk.aspx?xh=" + self.st_num + "&xm=" + self.st_urlName + "&gnmkdm=N121615"
self.session.headers['Referer'] = self.baseUrl + '/xs_main.aspx?xh=' + self.st_num
response2 = self.session.get(url_info_course_schedule)
html = response2.content.decode("gb2312")
soup = BeautifulSoup(html, 'html.parser')
self.__VIEWSTATE_info_course_schedule = soup.findAll('input')[2]['value']
url3_1 = self.baseUrl + "/xscjcx.aspx?xh=" + self.st_num + "&xm=" + self.st_urlName + "&gnmkdm=N121605"
self.session.headers['Referer'] = self.baseUrl + '/xs_main.aspx?xh=' + self.st_num
response3_1 = self.session.get(url3_1)
html = response3_1.content.decode("gb2312")
soup = BeautifulSoup(html, 'html.parser')
self.__VIEWSTATE3 = soup.findAll('input')[2]['value']
self.avail_grade_year = [item.get_text() for item in soup.find(id = 'ddlXN').find_all('option')]
self.avail_grade_year.remove('')
self.avail_grade_term = [item.get_text() for item in soup.find(id = 'ddlXQ').find_all('option')]
self.avail_grade_term.remove('')
# print(self.avail_grade_year)
# print(self.avail_grade_term)
print("Got essential information.")
pass
def latest_login_time(self):
ob_sql = data_conn()
ob_sql.start()
conn_new = ob_sql.conn
cursor = conn_new.cursor()
cursor.execute("select login_time from login_info WHERE stu_id = '%s'" % self.st_num)
conn_new.commit()
time_info = cursor.fetchone()
ob_sql.end()
return time_info[0]
def update_all_info( self ):
formatter_string = "%y-%m-%d"
time_info = self.latest_login_time()
time_info = date_time = datetime.datetime.strptime(time_info,'%Y-%m-%d')
time_info = time_info.date()
print(time_info)
now_time = datetime.date.today()
if (now_time - time_info).days < 2:
while 1:
ans = input("是否刷新数据?(y/n)\n")
if ans == 'y':
print("开始更新所有数据 ......")
break
elif ans == 'n':
return
else:
continue
print("开始更新课表")
for year in self.avail_courses_year:
for term in self.avail_courses_term:
print("更新第%s学年第%s学期中......" % (year,term))
responser = self.sp_courses_schedule(year,term)
courses = parserInfo.get_courses_schedule(responser)
if courses == []:
continue
# print(courses)
func.format_courses(courses)
print("课表更新完成")
print("开始更新成绩")
for year in self.avail_grade_year:
for term in self.avail_grade_term:
print("更新第%s学年第%s学期中......" % (year, term))
responser = self.sp_grades(year, term)
grades = parserInfo.get_grades(responser)
if grades == []:
continue
func.format_grades(grades)
# print(grades)
print("成绩更新完成")
pass
def choices( self,info = 'course' , type = 1):
'''info can only choice from course or grade, all of them must be a str'''
year = None; term = None
if info == 'course':
year = self.avail_courses_year
term = self.avail_courses_term
elif info == 'grade':
year = self.avail_grade_year
term = self.avail_grade_term
print()
for index,ayear in enumerate(year):
print('\t{0}:第{1}学年'.format(index+1,ayear), end = ' ')
print()
for index,aterm in enumerate(term):
print('\t{0:}:第{1}{2:<12}'.format(index+1,aterm,'学期'), end = ' ')
print(end = '\n\n')
while 1:
choice = input("请选择学年:\n")
if choice is not '':
try:
xn = year[int(choice) - 1]
break
except:
print("请输入数字")
continue
if type == 1:
while 1:
choice = input("请选择学期:\n")
if choice is not '':
try:
xq = term[int(choice) - 1]
break
except:
print("请输入数字")
continue
else:
while 1:
choice = input("请选择学期(可不输入):\n")
if choice is not '':
try:
xq = term[int(choice) - 1]
break
except:
print("请输入数字")
continue
else:
xq = ''
break
return xn,xq
def sp_grades(self,xn = None,xq = None):
# 选择学期
url3_1 = self.baseUrl + "/xscjcx.aspx?xh=" + self.st_num + "&xm=" + self.st_urlName + "&gnmkdm=N121605"
self.session.headers['Referer'] = self.baseUrl + "/xscjcx.aspx?xh=" + self.st_num + "&xm=" + self.st_urlName + "&gnmkdm=N121605"
data3 = {
"__EVENTTARGET":"",
"__EVENTARGUMENT":"",
"__VIEWSTATE":self.__VIEWSTATE3,
'hidLanguage':"",
"ddlXN":xn,
"ddlXQ":xq,
"ddl_kcxz":"",
"btn_xq" : u"学期成绩".encode('gb2312', 'replace')
}
response3 = self.session.post(url3_1,data=data3)
ans = response3.content.decode('GBK')
return ans.encode('utf-8')
def sp_GPA(self,xn = None,xq = None):
# 选择学期
print(xn, xq)
url3_1 = self.baseUrl + "/xscjcx.aspx?xh=" + self.st_num + "&xm=" + self.st_urlName + "&gnmkdm=N121605"
self.session.headers['Referer'] = self.baseUrl + "/xscjcx.aspx?xh=" + self.st_num + "&xm=" + self.st_urlName + "&gnmkdm=N121605"
data3 = {"__EVENTTARGET": "",
"__EVENTARGUMENT": "",
"__VIEWSTATE": self.__VIEWSTATE3,
'hidLanguage': "",
"ddlXN": xn,
"ddlXQ": xq,
"ddl_kcxz": "",
"Button1": u"成绩统计".encode('gb2312', 'replace')}
response3 = self.session.post(url3_1, data=data3)
ans = response3.content.decode('GBK')
return ans
def sp_courses_schedule(self,xn = None,xq = None):
self.session.headers[
'Referer'] = self.baseUrl + "/xsxkqk.aspx?xh=" + self.st_num + "&xm=" + self.st_urlName + "&gnmkdm=N121615"
data2 = {
'__EVENTTARGET': 'ddlXN',
'__EVENTARGUMENT': '',
'__VIEWSTATE': self.__VIEWSTATE_info_course_schedule,
'ddlXN': xn,
'ddlXQ': xq,
}
url2 = self.baseUrl + "/xsxkqk.aspx?xh=" + self.st_num + "&xm=" + self.st_urlName + "&gnmkdm=N121615"
response2 = self.session.post(url2, data = data2)
ans = response2.content.decode('GBK')
# print(ans)
return ans.encode('utf-8')
def sp_class(self, xn = None,xq = None):
# 选择学期
self.session.headers['Referer'] = self.baseUrl + "/xskbcx.aspx?xh=" + self.st_num + "&xm=" + self.st_urlName + "&gnmkdm=N121603"
data2 = {
'__EVENTTARGET':'xqd',
'__EVENTARGUMENT':'',
'__VIEWSTATE':self.__VIEWSTATE2,
'xnd':xn,
'xqd':xq,
}
url2 = self.baseUrl + "/xskbcx.aspx?xh=" + self.st_num + "&xm=" + self.st_urlName + "&gnmkdm=N121603"
response2 = self.session.get(url2,data = data2)
ans = response2.content.decode('GBK')
# print(ans)
return ans.encode('utf-8')