Python趣味爬蟲(chóng)之用Python實(shí)現(xiàn)智慧校園一鍵評(píng)教
一、安裝selenium庫(kù)
問(wèn)題1:什么是selenium模塊?
- 基于瀏覽器自動(dòng)化的一個(gè)模塊。
問(wèn)題2:selenium模塊有什么作用呢?
- 便捷地獲取網(wǎng)站中動(dòng)態(tài)加載的數(shù)據(jù)
- 便捷地實(shí)現(xiàn)模擬登錄
問(wèn)題3:環(huán)境安裝
pip install selenium
二、下載一個(gè)瀏覽器的驅(qū)動(dòng)程序(谷歌瀏覽器)
1.下載路徑
http://chromedriver.storage.googleapis.com/index.html
2.驅(qū)動(dòng)程序和瀏覽器的映射關(guān)系(谷歌瀏覽器)
方法1:[不推薦]
在瀏覽器地址欄輸入:chrome://version/

- 復(fù)制版本號(hào),只取前三節(jié)
示例:版本號(hào)為90.0.4430.212,只需復(fù)制90.0.4430
- 將復(fù)制的數(shù)字加到
https://chromedriver.storage.googleapis.com/LATEST_RELEASE_后面
示例:https://chromedriver.storage.googleapis.com/LATEST_RELEASE_90.0.4430
博主嘗試了沒(méi)有成功

方法2:[推薦]
安裝webdriver-manager庫(kù)
pip install webdriver-manager
運(yùn)行如下代碼
import time
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
# from webdriver_manager.microsoft import EdgeChromiumDriverManager
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
driver = webdriver.Chrome(ChromeDriverManager().install(),chrome_options=options)
# driver = webdriver.Edge(EdgeChromiumDriverManager().install())
driver.get('https://www.baidu.com/s?wd=123')
driver.close()
很簡(jiǎn)單,省很多事
三、智慧校園評(píng)教實(shí)現(xiàn)
1.新建python文件導(dǎo)入相關(guān)包
from selenium import webdriver import time from lxml import etree
2.使用selenium打開(kāi)登錄頁(yè)面
# 實(shí)例化一個(gè)瀏覽器對(duì)象
bro = webdriver.Chrome(executable_path='./chromedriver')# 驅(qū)動(dòng)程序所在路徑
# 讓瀏覽器發(fā)起一個(gè)指定url對(duì)應(yīng)請(qǐng)求
bro.get('http://sso.cqcet.edu.cn/login')

3.錄入用戶名密碼,點(diǎn)擊登錄按鈕實(shí)現(xiàn)登錄
# 標(biāo)簽定位
username_input = bro.find_element_by_id('username')
password_input = bro.find_element_by_id('password')
# 標(biāo)簽交互
username_input.send_keys('**********')# 智慧校園賬號(hào)
password_input.send_keys('**********')# 智慧校園密碼
# 點(diǎn)擊登入按鈕
btn = bro.find_element_by_class_name('logon-btn')
btn.click()
time.sleep(2)# 停頓2s

4.進(jìn)入教學(xué)評(píng)價(jià)系統(tǒng)
# 點(diǎn)擊學(xué)評(píng)教管理
bro.get('http://ossc.cqcet.edu.cn/xg/teaching/student/index/teach')
bro.find_element_by_class_name('nav-label').click()
time.sleep(2)
# 點(diǎn)擊學(xué)生評(píng)教
bro.get('http://ossc.cqcet.edu.cn/xg/teaching/student/xskb')
# page_source獲取瀏覽器當(dāng)前頁(yè)面的頁(yè)面源碼數(shù)據(jù)
page_text = bro.page_source

5.實(shí)現(xiàn)評(píng)教操作
# 解析onclick里面的內(nèi)容
tree = etree.HTML(page_text)
onclick_list = tree.xpath('//*[@id="bootstrap-table"]/tbody//a/@onclick')
print(onclick_list)
for onclick in onclick_list:
if onclick[0:15] != "checkEvaluation":
bro.execute_script(onclick)
time.sleep(1)
bro.find_element_by_class_name('layui-layer-btn0').click()
time.sleep(1)
time.sleep(5)
bro.quit()

6.完成效果圖

四、附錄
以下為實(shí)現(xiàn)谷歌無(wú)頭瀏覽器和反檢測(cè)代碼,供參考
from selenium import webdriver
from time import sleep
#實(shí)現(xiàn)無(wú)可視化界面
from selenium.webdriver.chrome.options import Options
#實(shí)現(xiàn)規(guī)避檢測(cè)
from selenium.webdriver import ChromeOptions
#實(shí)現(xiàn)無(wú)可視化界面的操作
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
#實(shí)現(xiàn)規(guī)避檢測(cè)
option = ChromeOptions()
option.add_experimental_option('excludeSwitches', ['enable-automation'])
#如何實(shí)現(xiàn)讓selenium規(guī)避被檢測(cè)到的風(fēng)險(xiǎn)
bro = webdriver.Chrome(executable_path='./chromedriver',chrome_options=chrome_options,options=option)
#無(wú)可視化界面(無(wú)頭瀏覽器) phantomJs
bro.get('https://www.baidu.com')
print(bro.page_source)
sleep(2)
bro.quit()
到此這篇關(guān)于Python趣味挑戰(zhàn)之用Python實(shí)現(xiàn)智慧校園一鍵評(píng)教的文章就介紹到這了,更多相關(guān)Python智慧校園一鍵評(píng)教內(nèi)容請(qǐng)搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信