【爬虫】(二)爬取西电教务处成绩

爬虫之西电教务处成绩测试代码,遇到验证码,已挂。

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
# -*-encoding:utf-8-*-
# coding=utf-8
__author__ = 'ysc'
import requests
from bs4 import BeautifulSoup
import xlrd
import xlwt
class ScrapeGrade:
def __init__(self, auth_url=None, log_url=None):
if not auth_url:
self.auth_url = "http://ids.xidian.edu.cn/authserver/login?service=http%3A%2F%2Fjwxt.xidian.edu.cn%2Fcaslogin.jsp"
self.log_url = "http://jwxt.xidian.edu.cn/caslogin.jsp"
else:
self.auth_url = auth_url
self.log_url = log_url
self.session = requests.Session()
def login(self, id='1601XXXXXX', password='XXXXX'):
r = self.session.get(self.auth_url)
data = r.text
bsObj = BeautifulSoup(data, "html.parser")
lt_value = bsObj.find(attrs={"name": "lt"})['value']
exe_value = bsObj.find(attrs={"name": "execution"})['value']
params = {'username': id, 'password': password,
"submit": "", "lt": lt_value, "execution": exe_value,
"_eventId": "submit", "rmShown": '1'}
headers = {'User-Agent': "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:44.0) Gecko/20100101 Firefox/44.0",
'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
# "Host": "ids.xidian.edu.cn",
"Accept-Language": "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3",
"Accept-Encoding": "gzip, deflate",
"Referer": "http://ids.xidian.edu.cn/authserver/login?service=http%3A%2F%2Fjwxt.xidian.edu.cn%2Fcaslogin.jsp",
# 'X-Requested-With': "XMLHttpRequest",
"Content-Type": "application/x-www-form-urlencoded"}
s = self.session.post(self.auth_url, data=params, headers=headers)
s = self.session.get(self.log_url)
def store_into_db_by_term(self):
# 按学期进行分类
import sqlite3
conn = sqlite3.connect('grades_term.db')
# conn.text_factory = str ##!!!
c = conn.cursor()
try:
# init the counter of the sheet
row = 0
# 打开成绩页面
grade_page = self.session.get("http://jwxt.xidian.edu.cn/gradeLnAllAction.do?type=ln&oper=qbinfo&lnxndm=2015-2016%D1%A7%C4%EA%B5%DA%D2%BB%D1%A7%C6%DA(%C1%BD%D1%A7%C6%DA)")
bsObj2 = BeautifulSoup(grade_page.text, "html.parser")
# datas 包含了所有学期的成绩, table
datas = bsObj2.find_all("table", attrs={"class": "titleTop2"})
# seme 指每学期的成绩. table
for i, seme in enumerate(datas):
#写入一行标题th
ths = seme.find_all('th')
titles = []
for col, th in enumerate(ths):
print(th.string.strip(), end=' ')
th = th.string.strip()
if th != '学分' and th != "成绩":
titles.append(th + r' text')
else:
titles.append(th + r' real')
# table.write(row, col, th.string.strip(), self.set_style('Times New Roman', 220, True))
# Create table
sent = '''CREATE TABLE {0} ( '''.format('table' + str(i+1))
for ith, title in enumerate(titles):
sent += title
if ith < len(titles) - 1:
sent += ", "
sent += ")"
try:
c.execute(sent)
conn.commit()
except sqlite3.OperationalError:
pass
print('\n')
row += 1
# 各科成绩
subs = seme.findAll('td', attrs={"align": "center"})
col_iter = 0
len_ths = len(ths)
grade_subs = []
# sub为具体的某科成绩
for sub in subs:
if sub.string:
if sub.string.strip() != '':
print(sub.string.strip(), end=' ')
grade_subs.append("'" + sub.string.strip()+"'")
else:
print("' '", end=' ')
grade_subs.append("' '")
else:
print(sub.find('p').string.strip(), end=' ')
grade_subs.append("'" + sub.find('p').string.strip() + "'")
col_iter += 1
if col_iter == len_ths:
# 此时一科的成绩以及visited, 该访问下一科
print('\n')
# Insert a row of data
sent = '''INSERT INTO {0} VALUES( '''.format('table' + str(i+1))
for ith, grade_sub in enumerate(grade_subs):
sent += grade_sub
if ith < len(grade_subs) - 1:
sent += ", "
sent += ")"
try:
c.execute(sent)
conn.commit()
except sqlite3.OperationalError as e:
print(e)
print(sent)
exit(-2)
row += 1
col_iter = 0
grade_subs = []
print("\n")
# 保存到xls中
finally:
conn.close()
def store_into_db_by_prop(self):
# 按科目属性(必修\选修)进行分类
import sqlite3
conn = sqlite3.connect('grades_prop.db')
c = conn.cursor()
try:
# init the counter of the sheet
row = 0
# 打开成绩页面
grade_page = self.session.get("http://jwxt.xidian.edu.cn/gradeLnAllAction.do?type=ln&oper=sxinfo&lnsxdm=001")
bsObj2 = BeautifulSoup(grade_page.text, "html.parser")
# datas 包含了所有学期的成绩, table
datas = bsObj2.find_all("table", attrs={"class": "titleTop2"})
# seme 指每学期的成绩. table
for i, seme in enumerate(datas):
#写入一行标题th
ths = seme.find_all('th')
titles = []
for col, th in enumerate(ths):
print(th.string.strip(), end=' ')
th = th.string.strip()
if th != '学分' and th != "成绩":
titles.append(th + r' text')
else:
titles.append(th + r' real')
# table.write(row, col, th.string.strip(), self.set_style('Times New Roman', 220, True))
# Create table
sent = '''CREATE TABLE {0} ( '''.format('table' + str(i+1))
for ith, title in enumerate(titles):
sent += title
if ith < len(titles) - 1:
sent += ", "
sent += ")"
try:
c.execute(sent)
conn.commit()
except sqlite3.OperationalError:
pass
print('\n')
row += 1
# 各科成绩
subs = seme.findAll('tr', attrs={'class': "odd"})
col_iter = 0
len_ths = len(ths)
grade_subs = []
# sub为具体的某科信息
for sub in subs:
infors = sub.findAll('td') #, attrs={"align": "center"})
for infor in infors:
if infor.string:
if infor.string.strip() != '':
print(infor.string.strip(), end=' ')
grade_subs.append("'" + infor.string.strip()+"'")
else:
print("' '", end=' ')
grade_subs.append("' '")
else:
infor = infor.find('p').string.strip()
if infor != '':
print(infor, end=' ')
grade_subs.append("'" + infor + "'")
else:
print("' '", end=' ')
grade_subs.append("' '")
# 此时一科的成绩已经visited, 该访问下一科
print('\n')
# Insert a row of data
sent = '''INSERT INTO {0} VALUES( '''.format('table' + str(i+1))
for ith, grade_sub in enumerate(grade_subs):
sent += grade_sub
if ith < len(grade_subs) - 1:
sent += ", "
sent += ")"
try:
c.execute(sent)
conn.commit()
except sqlite3.OperationalError as e:
print(e)
print(sent)
exit(-2)
row += 1
col_iter = 0
grade_subs = []
print("\n")
# 保存到xls中
finally:
conn.close()
def set_style(self, name, height, bold=False):
style = xlwt.XFStyle()
font = xlwt.Font()
font.name = name # 'Times New Roman'
font.bold = bold
font.color_index = 4
font.height = height
''' borders= xlwt.Borders() borders.left= 6 borders.right= 6 borders.top= 6 borders.bottom= 6 '''
style.font = font
# style.borders = borders
return style
def store_into_xls(self):
file = xlwt.Workbook()
table = file.add_sheet('grades', cell_overwrite_ok=True)
# init the counter of the sheet
row = 0
# 打开成绩页面
grade_page = self.session.get("http://jwxt.xidian.edu.cn/gradeLnAllAction.do?type=ln&oper=qbinfo&lnxndm=2015-2016%D1%A7%C4%EA%B5%DA%D2%BB%D1%A7%C6%DA(%C1%BD%D1%A7%C6%DA)")
bsObj2 = BeautifulSoup(grade_page.text, "html.parser")
# datas 包含了所有学期的成绩, table
datas = bsObj2.find_all("table", attrs={"class": "titleTop2"})
# seme 指每学期的成绩. table
for seme in datas:
#写入一行标题th
ths = seme.find_all('th')
for col, th in enumerate(ths):
print(th.string.strip(), end=' ')
table.write(row, col, th.string.strip(), self.set_style('Times New Roman', 220, True))
print('\n')
row += 1
# 各科成绩
subs = seme.findAll('td', attrs={"align": "center"})
col_iter = 0
len_ths = len(ths)
# sub为具体的某科成绩
for sub in subs:
if sub.string:
print(sub.string.strip(), end=' ')
table.write(row, col_iter, sub.string.strip())
else:
print(sub.find('p').string.strip(), end=' ')
table.write(row, col_iter, sub.find('p').string.strip())
col_iter += 1
if col_iter == len_ths:
print('\n')
row += 1
col_iter = 0
print("\n")
# 保存到xls中
file.save('chengji.xls')
if __name__ == '__main__':
# 初始化爬虫对象
sg = ScrapeGrade()
# 登录(在此处传入正确的个人学号与密码信息)
sg.login(id='1601XXXXXX', password='XXXXXX')
# 保存成绩为excel

谢谢你请我吃糖果!