天天看點

北理工大學生管理系統成績查詢爬蟲

1. 緣起 有學弟問起如何抓取js動态加載的網頁,具體的場景就是北理工大學生管理系統的菜單欄都是需要滑鼠放置到上面才會出現下拉菜單。我當時推薦用python的selenium包調PhantomJS去加載,通過move_to_element模拟滑鼠移動到網頁元素的過程。結果學弟試過之後發現下拉菜單并沒有出現,我當時懷疑是因為教務處的破系統隻能需要IE核心才能用的原因,于是決定自己試試,對比了一下PhantomJS的效果。 2. 方法 利用selenium包調用IE浏覽器加載網頁 3. 工具 python 2.7+selenium包+InternetExplorerDriver/PhantomJS 配置: pip install selenium InternetExplorerDriver: 從 http://selenium-release.storage.googleapis.com/index.html某個版本裡面下載下傳IEDriverServer 我自己是在2.53版本中下載下傳的IEDriverServer_x64_2.53.1.zip解壓後得到IEDriverServer.exe,請将其配置到環境變量裡,這裡我直接把它放到了Python安裝路徑下的Scripts(已配置到Path)中。 PhantomJS.exe: http://phantomjs.org/download.html 解壓後在bin目錄下找到phantomjs.exe,配置環境變量,同樣也可以放到python的Scripts目錄下。 4. 過程 a) 用PhantomJS嘗試 (略) 通過截圖發現move_to_element沒有成功 b) selenium+InternetExplorerDriver 首先得到IE浏覽器的執行個體 browser = webdriver.Ie() 第一步:完成登入 loginUrl = 'http://10.5.2.80/(l2ay3p55k13s5m45zkn1xy55)/default2.aspx' browser.get(loginUrl)

北理工大學生管理系統成績查詢爬蟲

完成表單 browser.find_element_by_name('TextBox1').send_keys(username) browser.find_element_by_name('TextBox1').send_keys(Keys.TAB) browser.find_element_by_name('TextBox2').send_keys(passwd) browser.find_element_by_name('TextBox2').send_keys(Keys.ENTER) 對應的頁面元素如下

北理工大學生管理系統成績查詢爬蟲
北理工大學生管理系統成績查詢爬蟲

等待網頁完成跳轉 由于這個跳轉過程可能很慢,是以通過WebDriverWait執行等待指令直至目标頁面某個元素出現為止 # 等待加載成功 locator = (By.CLASS_NAME, "MainMenu") WebDriverWait(browser, 50).until(EC.presence_of_element_located(locator)) 第二步:模拟滑鼠懸停 找到需要懸停的網頁元素 queryElement = browser.find_elements_by_class_name("MainMenu")[3] 利用ActionChains實作懸停 ActionChains(browser).move_to_element(queryElement).perform() 程式運作的實際效果

北理工大學生管理系統成績查詢爬蟲

擷取成績菜單進行跳轉 scoreElement = browser.find_element_by_id("SubMenuN1216").find_elements_by_class_name("SubMenu")[2] scoreElement.click() 對應的網頁源代碼

北理工大學生管理系統成績查詢爬蟲

浏覽器會彈出新的網頁 由于browser還停留在原有的視窗,是以需要将browser切換到新的視窗中。 # 保留目前視窗 nowHandle = browser.current_window_handle # 所有視窗 allHandles = browser.window_handles for handle in allHandles: if handle != nowHandle: #跳轉到新的視窗 browser.switch_to_window(handle)

北理工大學生管理系統成績查詢爬蟲

第三步查詢所有成績 點選擷取所有成績 allScoresBtn = browser.find_element_by_name("Button2") allScoresBtn.click() 等待成績出現 # 等待加載成功 locator = (By.CLASS_NAME, "datagridstyle") WebDriverWait(browser, 50).until(EC.presence_of_element_located(locator))

北理工大學生管理系統成績查詢爬蟲

讀取所有成績并寫入excel中 scoreTable = browser.find_element_by_class_name('datagridstyle') rows = scoreTable.find_elements_by_tag_name('tr') # 儲存資料到excel中 filename = username+'.xls' f = xlwt.Workbook() sheet1 = f.add_sheet(u'score', cell_overwrite_ok=True) for rowindex, row in enumerate(rows): cols = row.find_elements_by_tag_name('td') for colindex, col in enumerate(cols): text = col.text if text: text = text.strip().replace(' ', '')

sheet1.write(rowindex, colindex, text) f.save(filename) 對應的網頁源代碼為:

北理工大學生管理系統成績查詢爬蟲

最後的excel結果如下

北理工大學生管理系統成績查詢爬蟲

源代碼: BITCrawler

參考連接配接: 1. Selenium-擷取目前視窗句柄與切換回原視窗句柄 http://blog.sina.com.cn/s/blog_68f262210101vt2h.html 2. Python selenium 三種等待方式解讀 http://www.jb51.net/article/92684.htm 3. selenium_webdriver(python)滑鼠操作詳解 http://blog.csdn.net/u013372487/article/details/45368973 4. python+selenium浏覽器調用(chrome、ie、firefox) http://www.cnblogs.com/nzyjlr/p/4377663.html