1. 程式人生 > >100行代碼教你教務系統自動搶課!

100行代碼教你教務系統自動搶課!

username tab chrom 登錄 用戶 ssa inf while count

幫助廣大學生解決搶課問題!自動搶課!!

100行代碼幫你實現搶課!

? 本項目使用了python中splinter的API接口用來操作頁面交互,用了twilio用來給手機發送短信通知搶課成功。

? 歡迎大家來全球最大同性交友網站Github:https://github.com/xubin97
來fork我的菜雞代碼,希望你能來繼續增加更多功能,我也會不定期更新功能!

? 其中splinter API文檔鏈接:https://splinter.readthedocs.io/en/latest/mouse-interaction.html

加載各種包,例如splinter、time、twilio包等;

from selenium.webdriver.chrome.options import Options #初始化瀏覽器
from splinter.browser import Browser                  #加載splinter與頁面交互
from browsermobproxy import Server                    #瀏覽器驅動
from time import sleep                                #時間模塊
from twilio.rest import Client                        #向手機發送信息模塊
訪問一個頁面時可能也會訪問其他資源,例如js、css等,這時就要保證我們能獲取所有的請求。
#獲取所有網絡請求
server=Server("D:/splinter/browsermob-proxy-2/browsermob-proxy-2.1.4/bin/browsermob-proxy")
server.start()
proxy=server.create_proxy()
初始化瀏覽器,使其能訪問其他資源
chrome_options=Options()
chrome_options.add_argument(‘--proxy-server={host}:{port}‘.format(host=‘localhost‘,port=proxy.port))
#disable-infobars
書寫類,能完成登錄、訪問、篩選出要搶的課、循環點擊選課、搶課成功能發送短信通知你等功能
class HuoChe(object):

    """docstring for Train"""
    driver_name=‘Chrome‘
    executable_path=‘D:\爬蟲實戰\12306\chromedriver_win32‘
    #用戶名 密碼
    username = u"your_username"
    passwd = u"your_password"


    """網址"""
    # 強智教務系統登錄URL
    select_url = "http://............"
    # 強智選課頁面URL
    login_url = "http://............."


    def __init__(self):
        print("Welcome To Use The Tool")

#登錄函數
    def login(self):
        proxy.new_har()
        self.driver.visit(self.login_url)
        #填充密碼
        self.driver.fill("userAccount",self.username)
        #sleep(1)
        self.driver.fill("userPassword",self.passwd)
        print("等待驗證碼,自行輸入....")
        while True:
            if self.driver.url != self.initmy_url:
                sleep(1)
            else :
                break
#搶課成功,利用twilio發送短信的函數
    def send_message():
        account_sid="your_sid"
        auth_token="your_auth_totken"
        client=Client(account_sid,auth_token)

        client.messages.create(
        body=u" 搶課成功,請登錄查看 ",to="+86你註冊twilio時手機號",from_="+twilio分配給你的手機號")

#登錄進入,開始搶課
    def start(self):
        #選擇模擬的瀏覽器類型
        self.driver = Browser(driver_name=‘chrome‘)
        self.driver.driver.set_window_size(1400,1000)
        self.login()
        self.driver.visit(self.select_url)
        #選擇你想搶課的種類,名字或教學方式
        self.driver.find_by_text(u‘公選課選課‘).click()
        class_name=u"網絡授課"

        #利用iframe表格找到想選的課的選課按鈕,然後點擊
        #循環點擊所有要搶的課的選課id,當搶課成功發送短信通知
        if self.driver.find_by_id(‘mainFrame‘):
            with self.driver.get_iframe(‘mainFrame‘) as frame:
                b=frame.find_by_name("skls")
                b.fill(class_name)
                frame.find_by_value(u"查詢").click()

                #要搶課的id列表
                list=[......]
                #循環搶課,提示搶課成功後發送手機短信通知
                A=False
                while A==False:
                    for i in range(len(list)):
                        frame.find_by_id(list[i]).click()
                        with frame.get_alert() as alert:
                            alert.accept()
                            if alert.text=="選課失敗:此課堂選課人數已滿!":
                                alert.accept()
                                continue
                            elif alert.text=="選課成功":
                                alert.accept()
                                A=True
                                print(‘搶課成功‘)
                                #調用發送短信函數
                                self.send_message()
                                break
                            else:
                                alert.accept()
                                continue


if __name__=="__main__":
    train = HuoChe()
    train.start()

100行代碼教你教務系統自動搶課!