• 搓个UI界面

  • 打包

  • 多线程抢课

学校公选课抢课脚本,有查询功能

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import ddddocr
import requests
import schedule
import time
import base64

def login(url , username, password):
    bro.get(url)
    time.sleep(2)

    user_input = bro.find_element(By.XPATH,'/html/body/div[1]/div[2]/div[2]/div/div/div[2]/div/input')
    pw_input = bro.find_element(By.XPATH,'/html/body/div[1]/div[2]/div[2]/div/div/div[3]/div/input')

    user_input.send_keys(username)
    time.sleep(1)
    pw_input.send_keys(password)
    time.sleep(1)
    
    return bro


#验证码识别
def getvcode():
    ocr = ddddocr.DdddOcr()
    
    vscode_input = bro.find_element(By.XPATH,'/html/body/div/div[2]/div[2]/div/div/div[4]/div/div[1]/div/input')
    login_btn = bro.find_element(By.CLASS_NAME,'longin-button')
    
    img_path = bro.find_element(By.XPATH,'/html/body/div/div[2]/div[2]/div/div/div[4]/div/div[2]/img').get_attribute("src")
    img_path = img_path.split('data:image/png;base64,')[1]
    # print(img_path)
    vscode = base64.b64decode(img_path)
    
    with open('vscode.png', 'wb') as f:
        f.write(vscode)

    with open('vscode.png', 'rb') as g:     # 打开图片
        vscode_img = g.read()   
        
    strvcode = ocr.classification(vscode_img)
    vscode_input.send_keys(strvcode)
    time.sleep(1)
    login_btn.click()

    return strvcode


def chocourse():
    # print('enterchoose!!!')
    bro.refresh()
    time.sleep(1)
    course_btn=bro.find_element(By.XPATH,'/html/body/div[2]/div/div[1]/ul/li[2]')
    course_btn.click()
    time.sleep(2)
    search()


def retrylogin():
    while True:
        bro.refresh()
        login(aim_url['url'],aim_url['username'],aim_url['password']) 
        getvcode()
        checkpage()


def checkpage():
    xkpage = 'https://jwxk.xijing.edu.cn/xsxk/elective/grablessons?batchId=1c051ed52de34be79b55e163a421c928'
    try:
        buttonSure = WebDriverWait(bro, 2).until(
            EC.presence_of_element_located((By.XPATH,"/html/body/div[1]/div[4]/div/div[3]/span/button[1]"))
        )
        buttonSure.click()
        
        # print('click')
        time.sleep(2)
        
        buttonEnter = WebDriverWait(bro, 2).until(
            EC.presence_of_element_located((By.XPATH,"/html/body/div[1]/div[2]/div[2]/div/div/button"))
        )
        buttonEnter.click()
        # print('success')
        
    except Exception as e:
        print("按钮未出现,重新尝试:", e)
        time.sleep(1)
        retrylogin()
        
    if bro.current_url == xkpage:
        chocourse()
    else:
        retrylogin()
            

def search():
    search_input = bro.find_element(By.XPATH,'//*[@id="xsxkapp"]/div/div[3]/div[2]/div[3]/div/div[2]/div/input')
    search_btn = bro.find_element(By.XPATH,'/html/body/div[2]/div/div[3]/div[2]/button')
    search_input.send_keys(course_num['public1'])
    time.sleep(1)
    search_btn.click()
    time.sleep(1)
    pub1_btn = bro.find_element(By.XPATH,'/html/body/div[2]/div/div[3]/div[3]/div/div[1]/div[3]/table/tbody/tr[4]/td[9]/div/button')
    pub1_btn.click()
    time.sleep(1)
    pub1_Sure = bro.find_element(By.XPATH,'/html/body/div[3]/div/div[3]/button[2]')
    pub1_Sure.click()
    time.sleep(2)
    search_input.clear()
    time.sleep(1)
    search_input.send_keys(course_num['public2'])
    time.sleep(1)
    search_btn.click()
    time.sleep(1)
    pub2_btn = bro.find_element(By.XPATH,'/html/body/div[2]/div/div[3]/div[3]/div/div[1]/div[3]/table/tbody/tr/td[9]/div/button')
    pub2_btn.click()
    time.sleep(1)
    pub2_Sure = bro.find_element(By.XPATH,'/html/body/div[3]/div/div[3]/button[2]')
    pub2_Sure.click()
    time.sleep(2)
    search_input.clear()
    time.sleep(1)
    search_input.send_keys(course_num['public3'])
    time.sleep(1)
    search_btn.click()
    time.sleep(1)
    pub3_btn = bro.find_element(By.XPATH,'/html/body/div[2]/div/div[3]/div[3]/div/div[1]/div[3]/table/tbody/tr/td[9]/div/button')
    pub3_btn.click()
    time.sleep(1)
    pub3_Sure = bro.find_element(By.XPATH,'/html/body/div[3]/div/div[3]/button[2]')
    pub3_Sure.click()
    time.sleep(1)
    time.sleep(2)
    bro.close()
    print("选课完成")


def catchCourse():
    getvcode()
    checkpage()
#运行配置
if __name__ == "__main__":
    aim_url = {
        'url':'https://jwxk.xijing.edu.cn/xsxk/profile/index.html',

        #学号
        'username':'',

        #选课平台密码
        'password':''
    }

    #输入选课号码
    course_num = {
        'public1':'114514',
        #'public2':'选课号码',
        #'public4':'选课号码'
    }
    bro = webdriver.Edge()
    login(aim_url['url'],aim_url['username'],aim_url['password'])  
    catchCourse()