教你用Python實現全自動刷網課

專注的阿熊發表於2022-03-22

#引用庫

from selenium import webdriver

from selenium.webdriver.common.keys import Keys

from selenium.webdriver.common.by import By

import time

#全域性變數區域

url = "

driver = webdriver.Chrome()

account = '********@163.com'

password = '********'

lists=['1','2','3','4','5','6','7','8','9','10',]

cha='/html/body/div[4]/div[2]/div[4]/div[2]/div/div[1]/div/div/div[1]/div[1]/div/div[1]/div/div[2]/div'

chapters=['1','2','3','4','5','6','7','8','9','10','11','12','13','14']

les='/html/body/div[4]/div[2]/div[4]/div[2]/div/div[1]/div/div/div[1]/div[1]/div/div[2]/div/div[2]/div'

lessons=['1','2','3','4','5','6',]

#開啟網頁

driver.get(url)

driver.maximize_window()

#登陸部分

try:

    driver.find_element(By.XPATH, '/html/body/div[4]/div[2]/div[1]/div/div/div[1]/div[3]/div[3]/div').click()

    driver.find_element(By.XPATH, '/html/body/div[13]/div[2]/div/div/div/div/div[2]/span').click()

    print('點選登陸按鈕成功\n')

except:

    print('點選登陸按鈕時出錯')

#輸入賬號密碼部分

try:

    driver._switch_to.frame(0) #這裡轉到fame0 因為mooc網站點選登陸後是彈窗的

    driver.find_element(By.XPATH, '/html/body/div[2]/div[2]/div[2]/form/div/div[1]/div[2]/input').send_keys(account)

    driver.find_element(By.XPATH, '/html/body/div[2]/div[2]/div[2]/form/div/div[3]/div[2]/input[2]').send_keys(password)

    print('外匯跟單gendan5.com輸入賬號密碼成功\n')

    driver.find_element(By.XPATH, '/html/body/div[2]/div[2]/div[2]/form/div/div[8]/a').click()

except:

    print('登陸失敗')

time.sleep(2)

#點選 我的課程 按鈕

try:

    driver.switch_to.default_content()   driver.find_element(By.XPATH,'/html/body/div[4]/div[2]/div[1]/div/div/div[1]/div[3]/div[1]/img').click()

    print("點選頭像成功\n")

except:

    print("點選頭像出錯")

time.sleep(2)

#點選 高等數學(一) 課程

try:    driver.find_element(By.XPATH,'/html/body/div[4]/div[2]/div[3]/div/div[1]/div[3]/div/div[2]/div/div/div[2]/div[1]/div[2]/div/div[1]/div[1]/div[1]/a/div[1]/img').click()

    print("點選 '高等數學(一)'成功\n")

except:

    print("點選課程失敗")

time.sleep(2)

#點選繼續學習

try:

    driver.switch_to.window(driver.window_handles[-1])    driver.find_element(By.XPATH,'/html/body/div[4]/div[2]/div[4]/div[2]/div/div[1]/div/div[1]/div/div[1]/div/a[1]').click()

    print('點選繼續學習成功\n')

except:

    print("點選繼續學習失敗")

#瘋狂點選

#點選chapter後點選lesson 然後點選影片和文件

for c in chapters:

    time.sleep(1)

    js="document.getElementsByClassName('down f-bg j-list')[0].style.display='block'"

    driver.execute_script(js)

    driver.find_element(By.XPATH,cha+'['+c+']').click()

    time.sleep(1)

    for l in lessons:

        try:

            time.sleep(1)

            js2="document.getElementsByClassName('down f-bg j-list')[1].style.display='block'"

            driver.execute_script(js2)

            driver.find_element(By.XPATH,les+'['+l+']').click()

            time.sleep(1)

        except:

            break

        for i in lists:

            try:

                time.sleep(1)

driver.find_element(By.XPATH,'/html/body/div[4]/div[2]/div[4]/div[2]/div/div[1]/div/div/div[3]/ul/li['+ i +']' ).click()

                time.sleep(1)

            except:

                break


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2882989/,如需轉載,請註明出處,否則將追究法律責任。

相關文章