使用python的上海大学抢课方案

        此为自己写的抢课脚本,适用于课程限制人数后有人退课的情况。且可以在选课成功后给qq邮箱发送邮件(但需要qq等可以配置smtp的邮箱!)

####使用教程####

1.下载安装pycharm

2.下载chromedriver 并放在同一目录下

3.根据自己需要更改学号,课号等

4.芜湖,起飞!把电脑开着、链接校园网后挂着脚本就行

 如有bug请告知我,谢谢!(概率更新)

from selenium import webdriver

import time
import smtplib
from email.mime.text import MIMEText
from email.header import Header

name=#填入学号
passnum=#填入密码
emailname=#填入qq邮箱
emailpw=#输入邮箱密码(需要申请smtp,具体是什么自己网上查):选课成功了可以发邮件给邮箱

cid=['08305144','08305144']#课程号
tid=['1001','1005']#教师号

path=r'C:.\chromedriver.exe'#同文件夹下放一个chromedriver

option = webdriver.ChromeOptions()
#option.add_argument('headless')
option.add_experimental_option("excludeSwitches", ['enable-automation','enable-logging'])
driver = webdriver.Chrome(executable_path= path, chrome_options=option)

#登录
def login1():
    driver.get('https://oauth.shu.edu.cn/login/eyJ0aW1lc3RhbXAiOjE2MTA1MjQ5ODA1MTgwNDM2MDgsInJlc3BvbnNlVHlwZSI6ImNvZGUiLCJjbGllbnRJZCI6InlSUUxKZlVzeDMyNmZTZUtOVUN0b29LdyIsInNjb3BlIjoiIiwicmVkaXJlY3RVcmkiOiJodHRwOi8veGsuYXV0b2lzcC5zaHUuZWR1LmNuL3Bhc3Nwb3J0L3JldHVybiIsInN0YXRlIjoiIn0=')
    driver.find_element_by_name("username").click()
    driver.find_element_by_name("username").send_keys(name)
    driver.find_element_by_xpath("//*[@id=\"password\"]").click()
    driver.find_element_by_xpath("//*[@id=\"password\"]").send_keys(passnum)
    driver.find_element_by_xpath("//*[@id=\"submit-button\"]").click()
    driver.get('http://xk.autoisp.shu.edu.cn/Home/TermIndex')
    driver.find_element_by_xpath("/html/body/form/div/table/tbody").click()
    driver.find_element_by_xpath("/html/body/form/div/button").click()

#查询所有课程 若有空余的课 则输出i
def query1():
    while (1):
        for i in range(len(cid)):
            time.sleep(1)
            # 登录网页 输入课程号和教师号
            driver.get('http://xk.autoisp.shu.edu.cn/StudentQuery/QueryCourse')
            #退出登录就重新登录
            while (driver.current_url != 'http://xk.autoisp.shu.edu.cn/StudentQuery/QueryCourse'):
                login1()
                driver.get('http://xk.autoisp.shu.edu.cn/StudentQuery/QueryCourse')

            driver.find_element_by_name("CID").click()
            driver.find_element_by_name("CID").send_keys(cid[i])
            driver.find_element_by_name("TeachNo").click()
            driver.find_element_by_name("TeachNo").send_keys(tid[i])
            # 查询
            driver.find_element_by_xpath("//*[@id=\"QueryAction\"]").click()
            # a是容量 b是现在人数
            a = int(driver.find_element_by_xpath("//*[@id=\"divMainContent\"]/table/tbody/tr[2]/td[9]").text)
            b = int(driver.find_element_by_xpath("//*[@id=\"divMainContent\"]/table/tbody/tr[2]/td[10]").text)
            if(a>b):
                return i




cla=[]
#先登录
login1()
while(1):
    i=query1()
    #cla变成有空余的课程名
    cla=[driver.find_element_by_xpath("//*[@id=\"divMainContent\"]/table/tbody/tr[2]/td[2]").text]

    #去快速选课页面 输入课程号
    driver.get('http://xk.autoisp.shu.edu.cn/CourseSelectionStudent/FastInput')
    while (driver.current_url != 'http://xk.autoisp.shu.edu.cn/CourseSelectionStudent/FastInput'):
        login1()
        driver.get('http://xk.autoisp.shu.edu.cn/CourseSelectionStudent/FastInput')

    while(1):
        try:
            time.sleep(1)
            driver.find_element_by_name('cids[0]').click()
            driver.find_element_by_name('cids[0]').send_keys(cid[i])
            driver.find_element_by_name('tnos[0]').click()
            driver.find_element_by_name('tnos[0]').send_keys(tid[i])
            driver.find_element_by_xpath('//*[@id="FastInputAction"]').click()
        except Exception:
            print("error.2")
        else:
            break
    #获取提示信息
    ans=''
    while(1):
        try:
            ans=driver.find_element_by_xpath('//*[@id="divOperationResult"]/table/tbody/tr[2]/td[6]').text
            print(ans);
        except Exception:
            print("error.Last")
        else:
            break

    if '选课失败!已选' in ans :
        driver.get('http://xk.autoisp.shu.edu.cn/CourseReturnStudent/CourseReturn')
        while(driver.current_url != 'http://xk.autoisp.shu.edu.cn/CourseReturnStudent/CourseReturn'):
            login1()
            driver.get('http://xk.autoisp.shu.edu.cn/CourseReturnStudent/CourseReturn')
        nm='input[cid="'+cid[i]+'"][tno]'
        driver.find_element_by_css_selector(nm).click()
        driver.find_element_by_id("btnReturnClass").click()
        alert = driver.switch_to_alert()
        alert.accept()

    #选课成功 发邮件
    if '选课成功'in ans :
        print('选课成功',cla)
        message = MIMEText('Lesson Chosen Successfully', 'plain', 'utf-8')
        message['From'] = Header('Democracy<'+emailname+'>', 'utf-8')
        message['To'] =  Header('Democracy<'+emailname+'>', 'utf-8')
        message['Subject']=Header(cla[0],'utf-8')
        smtp = smtplib.SMTP_SSL('smtp.qq.com')
       # smtp.connect('smtp.qq.com',465)
        smtp.login(emailname, emailpw)
        smtp.sendmail(emailname, emailname, message.as_string())
        smtp.quit()
        tmp=cid[i]
        del cid[i]
        del tid[i]
        for j in range(len(cid)):
            if cid[j]==tmp:
                del cid[j]
                del tid[j]


time.sleep(3)
driver.quit()

你可能感兴趣的:(python,开发语言)