适用对象:江门市网络干部培训学院
功能:自动化批量选取指定页数范围内的课程。
使用方法:在终端输入账号密码后,浏览器自动弹出网站页面,15秒内输入验证码,然后输入开始和结束页数。
[Python] 纯文本查看 复制代码 # -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
import time
import re
import random
from time import sleep
driver = webdriver.Chrome(executable_path="chromedriver.exe")
def login(username,passwd):
nickName=driver.find_element_by_id('nickName')
sourcePassword=driver.find_element_by_id('sourcePassword')
login_btn=driver.find_element_by_id('login_btn')
nickName.send_keys(username)
sourcePassword.send_keys(passwd)
time.sleep(10)
login_btn.click()
time.sleep(random.uniform(2,5))
try:
driver.find_element_by_class_name("layui-layer-btn0").click()
except:
print('是否登录成功,若没成功登陆,请重新运行')
def course_need_Add(course_need_add):
course_need_add.click()
time.sleep(random.uniform(2,3))
course_need_add = driver.find_element_by_xpath('/html/body/div[3]/div[2]/div[1]/div[1]/div[5]/a') # 详情页的选课
course_need_add.click()
time.sleep(random.uniform(1,3))
driver.find_element_by_xpath('/html/body/div[6]/div[3]/a[1]').click()
time.sleep(random.uniform(1,3))
driver.find_element_by_xpath('/html/body/div[7]/div[3]/a').click()
driver.back()
return print('选课成功')
base_url = "https://gbpx.jiangmen.cn/"
username=input("请输入账号:")
passwd= input("请输入密码:")
print('请在浏览器网页登录界面账号密码自动填充后15秒内输入验证码')
driver.get(base_url)
driver.implicitly_wait(10)
now_window = driver.current_window_handle
login(username,passwd)
driver.implicitly_wait(15)
driver.find_element_by_id('navLesson').click()#进入课程中心
driver.find_element_by_xpath('/html/body/div[3]/div[2]/div[3]/div/div[2]/div[2]/div/div/a[6]').click()#点击尾页
time.sleep(3)
pageTotal=driver.find_element_by_xpath('/html/body/div[3]/div[2]/div[3]/div/div[2]/div[2]/div/div/a[6]').text#获取总页数
print ("总共有"+pageTotal+"页课程")
pageTotal=int(pageTotal)
page_start=input("请输入开始选课的页数(纯数字):")
page_end=input("请输入开始选课的页数(纯数字,不得大于总页数):")
for page in range(page_start,page_end):
number = driver.find_element_by_xpath('//*[@class="layui-input"]')
number.clear()
number.send_keys(page)
driver.find_element_by_xpath('//*[@class="layui-laypage-btn"]').click()
driver.implicitly_wait(10)
print ("第"+str(page)+"页")
for i in range(1,11):#对单个列表内单个课程信息进行判断是否需要继续学习。
xpath='/html/body/div[3]/div[2]/div[3]/div/div[2]/table/tbody/tr[{}]/td[5]/a'
couse_name='/html/body/div[3]/div[2]/div[3]/div/div[2]/table/tbody/tr[{}]/td[1]/a'
couse_name=couse_name.format(i)
xpath=xpath.format(i)
print(xpath)
time.sleep(random.uniform(1,3))
course_need_add = driver.find_element_by_xpath(xpath)
couse_name = driver.find_element_by_xpath(couse_name).text
print(course_need_add.text,couse_name)
if course_need_add.text == '已选':
i = i + 1
else:
course_need_Add(course_need_add)
time.sleep(random.uniform(1,3))
number = driver.find_element_by_xpath('//*[@class="layui-input"]')
number.clear()
number.send_keys(page)
driver.find_element_by_xpath('//*[@class="layui-laypage-btn"]').click()
time.sleep(5)
print("第" + str(page) + "页")
|