北京交通大学研究生教务处爬虫

本文介绍了一个简单的Python脚本,用于从北京交通大学研究生管理系统中爬取学生成绩。通过设置学号和密码,该脚本可以登录系统并获取历史成绩页面,然后使用正则表达式解析页面内容,提取学位课程的成绩和学分。
 1 import urllib
 2 import urllib2
 3 import requests
 4 import re
 5 
 6 student = 八位学号
 7 password = 密码
 8 postdata = urllib.urlencode({
 9     'u':student,
10     'p':password
11     })
12 
13 user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
14 headers = {'User-Agent' : user_agent }
15 
16 REQUEST = requests.session()
17 REQUEST.post(
18     url = 'http://gsdb.bjtu.edu.cn/client/login/',
19     data = postdata)
20 
21 ##get the scores
22 returnPage = REQUEST.get('http://gsdb.bjtu.edu.cn/score/history/')
23 
24 ##把成绩抓出来
25 reScore = re.compile(r'<tr>.*?</tr>', re.S)
26 resultList = reScore.findall(returnPage.text)
27 
28 Points = []
29 Scores = []
30 total = 0 
31 for res in resultList:
32     td = re.compile(r'<td>.*?</td>', re.S)
33     tdList = td.findall(res)
34     if (tdList[6]=='<td>学位课</td>'.decode('utf-8')):
35         num = re.compile(r'\d+')
36         point = num.findall(tdList[8])
37         score = num.findall(tdList[10])
38         Points.append((int)(point[0]))
39         Scores.append((int)(score[0]))
40         total += (int)(point[0]) * (int)(score[0])
41         
42 if sum(Points)!=0:
43     print str(student) + ' score is: ' + str(total / sum(Points))
44 else:
45     print 'Can\'t get scores'

 

转载于:https://www.cnblogs.com/asukayui/p/4418208.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值