Python调用浏览器实现课程表的获取

代码如下:

import matplotlib.pyplot as plt # plt 用于显示图片
import matplotlib.image as mpimg # mpimg 用于读取图片
def showimg(path):
    lena = mpimg.imread(path) # 读取和代码处于同一目录下的 lena.png
    plt.imshow(lena) # 显示图片
    plt.axis('off') # 不显示坐标轴
    plt.show()
def makeImgAndShow(WebElement):
    byte=WebElement.screenshot_as_png
    with open('test.png', "wb") as code:
        code.write(byte)
    showimg('test.png')
def gotoIframe(n):
    iframe = driver.find_elements_by_tag_name('iframe')[n]
    driver.switch_to.frame(iframe)# 最重要的一步
#     soup = bs4.BeautifulSoup(driver.page_source,"lxml")
def selectprint(selectsoup):
    for option in selectsoup.find_all('option'):
        print('{0} is {1}'.format(option['value'],option.text))
from urllib.parse import urljoin
from selenium import webdriver 
from selenium.webdriver.support.select import Select
import time
import bs4
url='http://xk.csust.edu.cn/ZNPK/KBFB_ClassSel.aspx'
driver = webdriver.Firefox()
driver.get(url) 
soup=bs4.BeautifulSoup(driver.page_source,'lxml')
time.sleep(1)
select1=driver.find_element_by_name('Sel_XNXQ')
select2=driver.find_element_by_name('Sel_XZBJ')
makeImgAndShow(select1)
selectprint(soup.find('select',{'name':'Sel_XNXQ'}))
Select(select1).select_by_value(input())
makeImgAndShow(select2)
selectprint(soup.find('select',{'name':'Sel_XZBJ'}))
Select(select2).select_by_value(input())
imgcode=driver.find_element_by_id('imgCode')
imgcode.click()
makeImgAndShow(imgcode)
keystr=input('請輸入验证码')
driver.find_element_by_id('txt_yzm').send_keys(keystr)
driver.execute_script("ChkValue()")
time.sleep(1)
driver.save_screenshot('map.png')
showimg('map.png')
gotoIframe(1)
soup=bs4.BeautifulSoup(driver.page_source,'lxml')
print(soup.prettify())

效果如下
Python调用浏览器实现课程表的获取_第1张图片
Python调用浏览器实现课程表的获取_第2张图片
Python调用浏览器实现课程表的获取_第3张图片
Python调用浏览器实现课程表的获取_第4张图片可惜大部分教务管理系统使用图片的查课方式,即使绕过了验证码,也不好采集数据。

这是非图片验证码的获取结果。

你可能感兴趣的:(Python,网络爬虫)