接上篇博客,这次改写的是第十讲:网络爬虫(十):一个爬虫的诞生全过程(以山东大学绩点运算为例)
由于没有山大的帐号,于是改爬北邮的教务,主要改动有以下几点:
1.python2改为python3
2.北邮使用统一登录平台,登录时会有一个随机数包含在input form里,name为lt,需要先get一下
3.登录以后需要get下左边栏的源码找到“课程成绩信息查询”所请求的url
教务页面如下:
源码如下:
import urllib.request
import http.cookiejar
from urllib.parse import urlparse
import re
#设置一个cookie处理器,它负责从服务器下载cookie到本地,并且在发送请求时带上本地的cookie
cj = http.cookiejar.LWPCookieJar()
cookie_support = urllib.request.HTTPCookieProcessor(cj)
opener = urllib.request.build_opener(cookie_support, urllib.request.HTTPHandler)
#urllib.request.install_opener(opener)
#需要POST的数据#
postdata={
'username':'username',
'password':'pwd',
'lt':'LT-3106955-1YQbER0OvvTcwn14aaHYAWB4GypLb5-1473765935975',#需要从页面取
'execution':'e1s1',
'_eventId':'submit',
'rmShown':'1'
}
#header
headers = {'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:14.0) Gecko/20100101 Firefox/14.0.1',
'Referer' : 'http://auth.bupt.edu.cn/authserver/login?service=http%3a%2f%2fyjxt.bupt.edu.cn%2fULogin.aspx'
}
#需要给Post数据编码
postData = urllib.parse.urlencode(postdata).encode('utf-8')
#自定义一个请求# 先get下 lt
req = urllib.request.Request(
'http://auth.bupt.edu.cn/authserver/login?service=http://yjxt.bupt.edu.cn/ULogin.aspx'
)
#访问该链接#
result = opener.open(req)
#打印返回的内容#
result=result.read().decode("utf-8")
#print (result)
#找到lt
ltItem=re.findall('<input.*?name="lt".*?value="(.*?)".*?/>',result,re.S)
print (ltItem[0])
print (postdata['lt'])
postdata['lt']=ltItem[0]
postData = urllib.parse.urlencode(postdata).encode('utf-8')
req = urllib.request.Request(
'http://auth.bupt.edu.cn/authserver/login?service=http://yjxt.bupt.edu.cn/ULogin.aspx',
postData,
headers
)
#访问该链接#
result = opener.open(req)
#打印返回的内容#
result=result.read().decode("utf-8")
#print (result)
# get下左边栏
req = urllib.request.Request(
'http://yjxt.bupt.edu.cn/Gstudent/leftmenu.aspx?UID=2015110871')
#访问该链接#
result = opener.open(req)
#打印返回的内容#
result=result.read().decode("utf-8")
#print (result)
#寻找课程成绩查询的url
#name: '课程成绩信息查询',t:'课程成绩信息查询',url:'Course/StudentScoreQuery.aspx?EID=l3PZkHTW3Su1WxpIyiJt7xg!oXf-MKDVwRsqQS-VmXPTktNXfJg51w==&UID=2015110871'
urlItem=re.findall('.*?课程成绩信息查询.*?url:\'(.*?)\'.*?',result,re.S)
print (urlItem[0])
#拼接url
gradeURL='http://yjxt.bupt.edu.cn/Gstudent/'+urlItem[0]
# get成绩信息
req = urllib.request.Request( gradeURL)
#访问该链接#
result = opener.open(req)
#打印返回的内容#
result=result.read().decode("utf-8")
print (result)
gradeItems=re.findall('.*?<td align="center">(.*?)</td>.*?<td align="center">(.*?)</td>.*?<td align="center">(.*?)</td>.*?<td align="center">(.*?)</td>.*?<td align="center">(.*?)</td>.*?<td align="center">(.*?)</td>.*?<td align="center">(.*?)</td>.*?</tr>',result,re.S)
for eachItem in gradeItems :
print ("%s\t%s\t%s\t%s\t%s\t%s\t%s\t" % (eachItem[0],eachItem[1],eachItem[2],eachItem[3],eachItem[4],eachItem[5],eachItem[6]))
运行效果如下:
3311100838 硕士英语(听说) 36 2 1 73 73
3311100839 硕士英语(写译) 36 2 1 73 73
3321100762 中国特色社会主义理论与实践研究 36 2 1 81 81
3131100141 高级操作系统 36 2 2 80 80
3131100008 面向对象技术与C++编程 36 2 1 80 88
3131100598 信息安全技术 36 2 1 87 79
3131100784 数论及其应用 36 2 1 77 77
3131100837 高级计算机体系结构与云计算 36 2 1 90 92
3311100704 自然辩证法概论 18 1 1 84 83
3411100473 图论及其应用 54 3 1 63 68
3131100621 形式语言与自动机 36 2 1 93 86
3711100493 网络协议分析与实现 36 2 2 76 76
3711100616 信息与网络安全 36 2 2 60 72