天天看点

北理工本科生管理系统成绩查询爬虫

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