python實現自動搶課指令碼

藉助pyautogui庫,我們可以輕鬆地控制滑鼠、鍵盤以及進行影象識別,實現自動搶課的功能

1。準備工作

我們在倉庫裡提供了2個必須的檔案,包括:

auto_get_lesson_pic_recognize.py

:指令碼檔案

info.xlsx

:執行操作資訊檔案

在執行這個指令碼(

auto_get_lesson_pic_recognize.py

)前,你需要:

1。安裝python併成功配置環境變數,可以在cmd下這樣檢查;若返回版本號,則已安裝

python ——version1

2。安裝以下的依賴,windows使用者請以管理員使用者執行cmd並依次執行:

pip install pyautoguipip install xlrd==1。2。0pip install pyperclippip install opencv-pythonpip install pillow 12345678910

到此,成功安裝了5個庫

2。配合使用py指令碼和xlsx檔案

第一步

需要將搶課的每一步所需要點選的

圖示/超連結

在頭腦中想清楚

第二步

將搶課每一步的所需點選的

圖示/超連結

截圖,儲存在和py指令碼同一路徑下

python實現自動搶課指令碼

開啟excel表格,根據第一行提示在單元格中進行輸入:

python實現自動搶課指令碼

A列————備註(可填可不填)

B列————操作型別,目前包括:

1。左鍵單擊(迴圈直到找到圖片為止):意思就是如果沒有找到你設定的那張圖片,它就一直找下去,找不到就不停;你所設定的次數是

找到成功的次數

2。輸入字串3。等待4。熱鍵5。左鍵單擊(無需找到圖片):找圖片不管找沒找到,就找那這麼多次,

次數=找到成功的次數+找到失敗的次數

C列————B列的引數

待點選圖示名(

包括圖片字尾名

,如。png)等待的時間(秒)輸入的字串熱鍵

D列————單擊重複次數

不填,預設為1若想無限單擊,填-1

按照你的選課步驟從第2行開始順序填寫excel表格的執行步驟

此時,儲存excel表格

第三步

我們開啟需要進行操作的選課網頁

我們在cmd下切換到指令碼所在目錄

D:cd xxpython auto_get_lesson_pic_recognize。py12345

根據提示執行即可

上圖示例

python實現自動搶課指令碼

3。auto_get_lesson_pic_recognize功能介紹

(1)。搶課一次

注意

截圖時請隨機應變,匹配到影象後,滑鼠自動點選影象正中央,建議配合qq截圖,ctrl+a/t+a,選取一個獨一無二的標記在截圖中並且將所要點選的點放在qq截圖四個藍點的中央

python實現自動搶課指令碼

如果遇到同一畫面中需要點選的圖示存在多個一樣的,沒有特徵參照物,可以在那一步設定等待若干秒,手動點選圖示

若未成功識別圖片,將迴圈執行識別操作;手動點選圖示成功,excel表格中中的指令也會跳到下一條

考慮到網路延遲問題,建議合理利用等待功能

(2)。蹲點撿漏

在搶課一次的基礎上套了一層死迴圈

巧妙利用f5、左鍵單擊(迴圈直到找到圖片為止)、左鍵單擊(無需找到圖片),可以24h掛機實現蹲點撿漏

請發揮你的聰明才智,正確截圖

python實現自動搶課指令碼

4。座標版本(不建議使用)

座標版本位於

coordinate_version

目錄下

如果能夠確切知道所點選的位置的座標,可以選用座標版本

配合qq截圖,你能夠輕鬆知道你的滑鼠在1920×1080解析度下在螢幕上的座標(以畫素為單位)

順序排列單擊位置的座標,實現搶課

excel表格中根據提示填寫座標、操作

5。程式碼

import pyautoguiimport timeimport xlrdimport pyperclipdef Mouse(click_times, img_name, retry_times): if retry_times == 1: location = pyautogui。locateCenterOnScreen(img_name, confidence=0。9) if location is not None: pyautogui。click(location。x, location。y, clicks=click_times, duration=0。2, interval=0。2) elif retry_times == -1: while True: location = pyautogui。locateCenterOnScreen(img_name,confidence=0。9) if location is not None: pyautogui。click(location。x, location。y, clicks=click_times, duration=0。2, interval=0。2) elif retry_times > 1: i = 1 while i < retry_times + 1: location = pyautogui。locateCenterOnScreen(img_name,confidence=0。9) if location is not None: pyautogui。click(location。x, location。y, clicks=click_times, duration=0。2, interval=0。2) print(“重複{}第{}次”。format(img_name, i)) i = i + 1def WorkFunction1(sheet): i = 1 while i < sheet。nrows: cmd_type = sheet。cell_value(i, 1) if cmd_type == 1。0: img_name = sheet。cell_value(i, 2) retry_times = 1 if sheet。cell_type(i, 3) == 2 and sheet。cell_value(i, 3) != 0: retry_times = sheet。cell_value(i, 3) Mouse(1, img_name, retry_times) print(“單擊左鍵:{} Done”。format(img_name)) elif cmd_type == 2。0: string = sheet。cell_value(i, 2) pyperclip。copy(string) pyautogui。hotkey(‘ctrl’,‘v’) print(“輸入字串:{} Done”。format(string)) elif cmd_type == 3。0: wait_time = sheet。cell_value(i, 2) time。sleep(wait_time) print(“等待 {} 秒 Done”。format(wait_time)) elif cmd_type == 4。0: hotkey = sheet。cell_value(i, 2) time。sleep(1) pyautogui。hotkey(hotkey) print(“按下 {} Done”。format(hotkey)) time。sleep(1) i = i + 1def WorkFunction2(sheet) : while True: WorkFunction1(sheet) time。sleep(2)if __name__ == ‘__main__’: start_time = time。time() file = “info。xlsx” xr = xlrd。open_workbook(filename=file) sheet = xr。sheet_by_index(0) print(“————歡迎使用自動搶課指令碼————”) print(“————-@danteking————-”) print(“1。搶課一次”) print(“2。蹲點等人退課後搶指定課”) choice = input(“>>”) start_time = time。time() if choice == “1”: WorkFunction1(sheet) elif choice == “2”: WorkFunction2(sheet) else: print(“非法輸入,退出”) end_time = time。time() time_consume = end_time - start_time time_consume = (‘%。2f’ % time_consume) print(“耗時 {} 秒”。format(time_consume))123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101