爬虫_西电研究生教务系统_技术文档
教務系統爬蟲工作初步完成
關于教務系統的一系列爬取工作已經初步完成,Holi爬蟲組的工作也算正式進入優化階段。
我們需要根據后臺組的需要,轉換成CVS或數據庫形式。需要和后臺組進行商量。
實現的功能
模擬登陸
- 此為爬取數據的第一步,之前試過很多方法,遇到的問題也各種各樣。
問題的解決:
模擬登陸需要很多東西,一定要根據抓包來進行數據分析,分析報頭的組成形式,再模擬報頭所需的東西進行模擬post。
模擬登陸作為爬取教務系統的第一步,花的時間也比較久。在學習模擬登陸上也花了很多時間。
通過此次模擬登錄的實現,了解到了從發送其請求,到瀏覽器解析出的網頁的整個過程。
魯棒性問題:
之前缺少所需的報頭消息而意外的觸發了教務系統的驗證碼機制。現在報頭已經完整,只要學號密碼正確是不會觸發驗證碼機制的。但是,如果學號或密碼輸入錯誤,是會觸發驗證碼機制的。簡單的驗證碼可以使用Python來進行OCR識別,但是教務處的驗證碼比較復雜。此問題的解決方案暫定為講驗證碼圖片呈現給用戶,讓其進行手動輸入。
課表的爬取
課表的行列組合比較復雜,這里只是簡單的把課表消息從HTML中解析出來。
后期的工作重點仍在HTML解析和數據處理方面。需要和后臺組溝通。
課表的技術文檔在上一次文檔中簡單講過,現在為了整體閱讀性,將其搬移過來:
1、init():兩個URL分別為用抓包軟件獲取的實際登錄網址和實際提交賬號密碼的網址。
2、login():用抓包軟件獲取的用Chrome瀏覽器登錄教務處的head報文,login()為模擬登錄教務處的所需信息。
3、Print():將登錄進去的課程表HTML網頁打印出來。
4、使用前請確認安裝BeautifulSoup模塊。請修改里面的學號id和password再進行運行。
5、后續將從HTML網頁中提取出有用的信息。
6、運行login()后便可以登錄進教務處系統,修改Print()中的URL即可完成不同信息的獲取,比如換成成績網頁的URL、教務通知的URL等等。
成績查詢
在 Print 函數中將 get 的網址換成課表的網址即可進行爬取。
對返回的HTML進行了簡單的解析,提取了簡單的標簽,獲取的數據可讀性不是很強。
與課表的問題一樣,后期的重點仍在數據處理。
通知公告的爬取
在西電,最令人煩惱就是你今天去上課了,可是老師翹課了!!!
一般情況下學生是不會經常去刷教務處網頁的,但是教務系統有一個滾動的通知公告。
只要有老師在上面發布調課通知或考試通知,上面就顯示。
這上面的公告是全校性的。
同理,這個網頁的解析提取比較簡單,數據看起來也很和諧。
我的消息
只要用戶的老師翹課或者出成績,個人就會收到該通知。
這個功能的實現,也可以解決后臺組一直提倡的個性化推送,教務處已經幫咱們實現了,哈哈哈!
后期我們只需要隔斷時間判斷是否有新的通知產生即可得到最新的消息,彈窗通知給用戶即可。
待解決的問題
數據處理!!這個的工作量和爬取來比還是比較大的。
但是已經邁出了第一步,后面的路會好走許多。
come on !
附代碼,作備份。
1、課表
#Python35 爬蟲 西電 研究生教務處 課表 #注:請修改login()學號密碼進行爬取 #肖灑 2017/1/19 V1.0 # -*-encoding:utf-8-*- # coding=utf-8 __author__ = 'ysc' import requests import csv from bs4 import BeautifulSoupclass 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_urlself.log_url = log_urlself.session = requests.Session()def login(self, id='學號', password='密碼'):r = self.session.get(self.auth_url)data = r.textbsObj = 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","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","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 Print(self):grade_page = self.session.get("http://yjsxt.xidian.edu.cn/eduadmin/findCaresultByStudentAction.do")bsObj2 = BeautifulSoup(grade_page.text, "html.parser")nameList = bsObj2.findAll("td", {"class":"textCenter"})for name in nameList:print(name.get_text())if __name__ == '__main__':# 初始化爬蟲對象sg = ScrapeGrade()# 登錄(在此處傳入正確的個人學號與密碼信息)sg.login(id='學號', password='密碼')sg.Print()2、通知公告
#Python35 爬蟲 西電研究生教務處 通知公告 #注:請修改 login()學號密碼進行爬取 #肖灑 2017/1/25 V1.0 # -*-encoding:utf-8-*- # coding=utf-8 __author__ = 'ysc' import requests import csv from bs4 import BeautifulSoupclass 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_urlself.log_url = log_urlself.session = requests.Session()def login(self, id='學號', password='密碼'):r = self.session.get(self.auth_url)data = r.textbsObj = 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","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","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 Print(self):grade_page = self.session.get("http://yjsxt.xidian.edu.cn/info/findAllBroadcastMessageAction.do?flag=findAll")bsObj2 = BeautifulSoup(grade_page.text, "html.parser")nameList = bsObj2.findAll("td", {"class":"textTop"})for name in nameList:print(name.get_text())if __name__ == '__main__':# 初始化爬蟲對象sg = ScrapeGrade()# 登錄(在此處傳入正確的個人學號與密碼信息)sg.login(id='學號', password='密碼')sg.Print()3、成績查詢
#Python35 爬蟲 西電研究生教務處 成績查詢 #注:請修改login()學號密碼進行爬取 #肖灑 2017/02/01 V1.0 # -*-encoding:utf-8-*- # coding=utf-8 __author__ = 'ysc' import requests import csv from bs4 import BeautifulSoupclass 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_urlself.log_url = log_urlself.session = requests.Session()def login(self, id='學號', password='密碼'):r = self.session.get(self.auth_url)data = r.textbsObj = 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","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","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 Print(self):grade_page = self.session.get("http://yjsxt.xidian.edu.cn/queryScoreByStuAction.do")bsObj2 = BeautifulSoup(grade_page.text, "html.parser")nameList = bsObj2.findAll("td")for name in nameList:print(name.get_text())if __name__ == '__main__':# 初始化爬蟲對象sg = ScrapeGrade()# 登錄(在此處傳入正確的個人學號與密碼信息)sg.login(id='學號', password='密碼')sg.Print()4、我的消息
#Python35 爬蟲 西電研究生教務處 我的消息(系統消息、成績提示等) #注:請修改login()學號密碼進行爬取 #肖灑 2017/02/01 V1.0 # -*-encoding:utf-8-*- # coding=utf-8 __author__ = 'ysc' import requests import csv from bs4 import BeautifulSoupclass 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_urlself.log_url = log_urlself.session = requests.Session()def login(self, id='學號', password='密碼'):r = self.session.get(self.auth_url)data = r.textbsObj = 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","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","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 Print(self):grade_page = self.session.get("http://yjsxt.xidian.edu.cn/info/findAllMessageAction.do")bsObj2 = BeautifulSoup(grade_page.text, "html.parser")nameList = bsObj2.findAll("li")for name in nameList:print(name.get_text())if __name__ == '__main__':# 初始化爬蟲對象sg = ScrapeGrade()# 登錄(在此處傳入正確的個人學號與密碼信息)sg.login(id='學號', password='密碼')sg.Print()總結
以上是生活随笔為你收集整理的爬虫_西电研究生教务系统_技术文档的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: FHC2022食饮魅力 势不可挡
- 下一篇: 邱淑贞的女儿太漂亮啦,邱淑贞为她女儿站台