最近看到隔壁室友在學(xué)習(xí)python,再加上那句“爬蟲(chóng)學(xué)得好,監(jiān)獄進(jìn)的早”,于是有了一個(gè)大膽的想法,刷網(wǎng)課。我是通過(guò)使用selenium驅(qū)動(dòng)真實(shí)的瀏覽器來(lái)實(shí)現(xiàn)
1.自動(dòng)點(diǎn)擊播放
2.自動(dòng)切換到流暢畫(huà)質(zhì)(省流量)
3.自動(dòng)1.5倍速
4.自動(dòng)靜音
4.自動(dòng)檢查當(dāng)前播放時(shí)間,結(jié)束后調(diào)到下一集
由于是驅(qū)動(dòng)真實(shí)的瀏覽器來(lái)實(shí)現(xiàn)一系列操作,所以理論上不會(huì)有封號(hào)危險(xiǎn) (祝你好運(yùn))
- # _*_ codeing utf-8 _*_
- # @TIME : 2021/11/6 22:30
- # @Author : wust Qiuyu
- # @File : test_selenium
- # @Project : 代碼
- from selenium import webdriver
- from selenium.webdriver.common.by import By
- import time
- from selenium.webdriver import ActionChains
- from selenium.webdriver.chrome.options import Options
- # 無(wú)界面
- def share_browser(): # 初始化
- chrome_options = Options()
- chrome_options.add_argument('--headless')
- chrome_options.add_argument('--disable-gpu')
- # 瀏覽器的安裝路徑 打開(kāi)文件位置 #這個(gè)路徑是你谷歌瀏覽器的路徑
- # chrome瀏覽器的安裝位置 Chromedriver需在當(dāng)前py文件的相同路徑下
- path = r'C:\Users\19436\AppData\Local\Google\Chrome\Application\chrome.exe'
- chrome_options.binary_location = path
- browser = webdriver.Chrome(chrome_options=chrome_options)
- return browser
- # 取消下方注釋即可使用有界面
- # # # # # # # # # # # # # # # # # # # # # #
- # 加載驅(qū)動(dòng) 有界面 #
- path = './chromedriver.exe' #
- browser = webdriver.Chrome(path) #
- # # # # # # # # # # # # # # # # # # # # # #
- # # # # # # # # # # # # # # # # # # # # # #
- # # 加載驅(qū)動(dòng) 無(wú)界面 #
- # browser = share_browser() #
- # # # # # # # # # # # # # # # # # # # # # #
- # 答題
- def response():
- try:
- #檢查是否有彈窗檢驗(yàn)
- browser.find_element(By.XPATH,'//div[@id="app"]/div[1]/div[7]/div[@class="el-dialog"]')
- option = browser.find_elements(By.XPATH, '//ul[@class="topic-list"]/li')
- for i in range(len(option)):
- option[i].click()
- # 獲取正確答案
- answer = browser.find_element(By.XPATH,"http://p[@class='answer']/span").text.split(',')
- print('正確答案是:' + str(answer))
- # 點(diǎn)擊正確答案
- for i in range(len(option)):
- print('選項(xiàng)'+str(i)+':'+option[i].text)
- flag=False
- for j in range(len(answer)):
- if option[i].text.find(answer[j])!=-1:
- flag=True
- break
- if(flag==False):
- option[i].click()
- # 點(diǎn)擊關(guān)閉
- close = browser.find_element(By.XPATH,'//div[@class="btn"]')
- close.click()
- print("答題成功!\t\t\t√")
- play()
- return True
- except:
- print("未找到彈窗測(cè)驗(yàn)\t\t?")
- return False
- # 點(diǎn)擊我知道了
- def clickIknow():
- try:
- # 檢測(cè)是否有我知道了
- browser.find_element(By.XPATH,'//div[@id="app"]/div[1]/div[6]')
- # 點(diǎn)擊我知道了
- iKnow = browser.find_element(By.XPATH,'//div[@id="app"]/div[1]/div[6]/div[1]/div[3]/span/button')
- iKnow.click()
- print("關(guān)閉我知道成功!\t√")
- return True
- except:
- print("未發(fā)現(xiàn)我知道了\t\t?")
- return False
- # 點(diǎn)擊學(xué)前必讀
- def clickBeforeRead():
- try:
- # 檢測(cè)是否有學(xué)前必讀
- browser.find_element(By.XPATH,'//div[@id="app"]/div[1]/div[7]')
- # 點(diǎn)擊×
- close = browser.find_element(By.XPATH,'//div[@id="app"]/div[1]/div[7]/div[2]/div[1]/i')
- close.click()
- print("關(guān)閉學(xué)前必讀成功!\t√")
- return True
- except:
- print("未發(fā)現(xiàn)學(xué)前必讀\t\t?")
- return False
- # 獲取當(dāng)前播放時(shí)間
- def getCurrentTime():
- openControlsBar()
- time = browser.find_element(By.XPATH,'//div[@class="nPlayTime"]/span[1]').text
- print('當(dāng)前播放時(shí)間為:'+time)
- return time
- # 獲取當(dāng)前視頻總時(shí)間
- def getTotalTime():
- openControlsBar()
- time = browser.find_element(By.XPATH,'//div[@class="nPlayTime"]/span[2]').text
- print('本集視頻時(shí)間為:' + time)
- return time
- # 播放下一個(gè)視頻
- def nextVideo():
- openControlsBar()
- nextBtn = browser.find_element(By.XPATH,'//div[@id="nextBtn"]')
- nextBtn.click()
- print('\n切換下一個(gè)視頻\n')
- # 1.5速度
- def fasterPlay():
- try:
- openControlsBar()
- speed = browser.find_element(By.XPATH,' //div[@class="speedBox"]/div/div[1]')
- speedbox = browser.find_element(By.XPATH,'//div[@class="speedBox"]')
- time.sleep(5)
- openControlsBar()
- ActionChains(browser).move_to_element(speedbox).perform()
- speed.click()
- print('成功切換成'+speed.text+'倍速\t√')
- except:
- print('切換成' + speed.text + '倍速失敗,稍后將重試\t?')
- # 點(diǎn)擊開(kāi)始播放
- def play():
- start_status = browser.find_element(By.XPATH,'//div[@id="playButton"]').get_attribute('class')
- start_button = browser.find_element(By.XPATH,'//div[@id="playButton"]')
- time.sleep(10)
- if start_status.find('playButton')!=-1:
- print('當(dāng)前靜止')
- openControlsBar()
- time.sleep(1)
- start_button.click()
- print('點(diǎn)擊播放成功\t\t√')
- # 點(diǎn)擊靜音
- def noVoice():
- try:
- voice_status = browser.find_element(By.XPATH,'//div[@id="vjs_container"]/div[10]/div[6]').get_attribute('class')
- voice_buttton= browser.find_element(By.XPATH,'//div[@id="vjs_container"]/div[10]/div[6]/div[1]')
- print(voice_status)
- if voice_status.find('volumeNone')==-1:
- print('此時(shí)非靜音')
- time.sleep(5)
- openControlsBar()
- voice_buttton.click()
- print('靜音成功\t\t\t√')
- except:
- print('靜音失敗,稍后將重試\t\t?')
- # 切換流暢畫(huà)質(zhì)
- def changeVideoQuality():
- try:
- openControlsBar()
- videoQuality = browser.find_element(By.XPATH,' //div[@class="definiBox"]/div/b[1]')
- definiBox = browser.find_element(By.XPATH,' //div[@class="definiBox"]')
- time.sleep(5)
- openControlsBar()
- ActionChains(browser).move_to_element(definiBox).perform()
- videoQuality.click()
- print('成功切換到流暢畫(huà)質(zhì)\t√')
- checkWindow()
- except:
- print('切換到流暢畫(huà)質(zhì)失敗,稍后將重試\t\t?')
- # 打開(kāi)ControlsBar
- def openControlsBar():
- checkWindow()
- controlsBar = browser.find_element(By.XPATH,' //div[@id="container"]')
- ActionChains(browser).move_to_element(controlsBar).perform()
- # 檢測(cè)彈窗
- def checkWindow():
- print(time.strftime('%Y-%m-%d %H:%M:%S')+'\n'+'正在播放:'+getUnitName())
- clickIknow()
- clickBeforeRead()
- response()
- print('\n\n')
- # 獲取當(dāng)前章節(jié)
- def getUnitName():
- try:
- name = browser.find_element(By.XPATH,'//*[@id="app"]/div/div[2]/div[1]/div[1]/div[1]')
- return name.text
- except:
- return '暫未加載'
- if __name__ == '__main__':
- # 訪問(wèn)網(wǎng)站 課程的地址
- url = 'https://studyh5.zhihuishu.com/videoStudy.html#/studyVideo?recruitAndCourseId=4b595c5c435d4859454a5859584259455f'
- # 賬號(hào)密碼
- userName = "**************"
- passWord = "**************"
- print(time.strftime('%Y-%m-%d %H:%M:%S')+'\n'+'開(kāi)始學(xué)習(xí)\n')
- browser.get(url)
- # 元素定位 輸入用戶名密碼
- print(time.strftime('%Y-%m-%d %H:%M:%S')+'\n'+'正在輸入賬號(hào)\n')
- inputUsername = browser.find_element(By.XPATH,'//input[@id="lUsername"]')
- inputUsername.send_keys(userName)
- print(time.strftime('%Y-%m-%d %H:%M:%S')+'\n'+'賬號(hào)輸入成功\n')
- print(time.strftime('%Y-%m-%d %H:%M:%S')+'\n'+'正在輸入密碼\n')
- inputPassword = browser.find_element(By.XPATH,'//input[@id="lPassword"]')
- inputPassword.send_keys(passWord)
- print(time.strftime('%Y-%m-%d %H:%M:%S')+'\n'+'密碼輸入成功\n')
- # 點(diǎn)擊登錄
- print(time.strftime('%Y-%m-%d %H:%M:%S')+'\n'+'正在登陸\n')
- login = browser.find_element(By.XPATH,"http://span[@class='wall-sub-btn']")
- login.click()
- print(time.strftime('%Y-%m-%d %H:%M:%S')+'\n'+'登陸成功\n')
- print(time.strftime('%Y-%m-%d %H:%M:%S')+'\n'+'正在進(jìn)入課程\n\n')
- print('----------------------------------')
- import threading
- from threading import Lock,Thread
- import time,os
- while(True):
- # 有時(shí)進(jìn)入視頻會(huì)直接彈出窗口,且窗口顯示順序不定,故檢測(cè)三次,保證再開(kāi)始播放之前關(guān)閉
- print(time.strftime('%Y-%m-%d %H:%M:%S')+'\n'+'正在在執(zhí)行視頻播放前檢查')
- for i in range(3):
- print('\n第' + str(i) + '檢測(cè)')
- checkWindow()
- time.sleep(2)
- print(time.strftime('%Y-%m-%d %H:%M:%S')+'\n檢查完畢\n\n\n開(kāi)始優(yōu)化播放\n')
- # 避免在前三次中未檢測(cè)出窗口,開(kāi)始操作前全部檢查一次
- checkWindow()
- play()
- print('正在修改畫(huà)質(zhì)')
- time.sleep(2)
- checkWindow()
- changeVideoQuality()
- print('畫(huà)質(zhì)修改成功\t\t\t√')
- print('正在修該播放速度')
- checkWindow()
- fasterPlay()
- print('播放速度修改成功\t\t\t√')
- print('正在修改靜音')
- checkWindow()
- noVoice()
- print('靜音修改成功\t\t√\n\n')
- # 每隔5秒檢測(cè)是否有我知道了,學(xué)前必讀,是否播放完
- while(True):
- print(time.strftime('%Y-%m-%d %H:%M:%S'))
- # 播放完畢,下一集
- if getCurrentTime() == getTotalTime():
- print('\n\n')
- checkWindow()
- nextVideo()
- break
- print('\n\n')
- checkWindow()
- print('\n\n')
- time.sleep(5)
聯(lián)系客服