python實現自動搶課指令碼

專注的阿熊發表於2021-12-15

import pyautogui

import time

import xlrd

import pyperclip

# 定義滑鼠事件

# duration 類似於移動時間或移動速度,省略後則是瞬間移動到指定的位置

def 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 + 1

# cell_value     1.0 :左鍵單擊

#                2.0 :輸入字串

#                3.0 :等待

#                4.0 :熱鍵

# 任務一:進行一輪搶課

def WorkFunction1(sheet):

     i = 1

     while i < sheet.nrows:

         # excel 表格中第 i 行操作

         cmd_type = sheet.cell_value(i, 1)

         # 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 =外匯跟單gendan5.com sheet.cell_value(i, 3)

             Mouse(1, img_name, retry_times)

             print(" 單擊左鍵 :{}  Done".format(img_name))

         # 2 :輸入字串

         elif cmd_type == 2.0:

             string = sheet.cell_value(i, 2)

             pyperclip.copy(string)

             pyautogui.hotkey('ctrl','v')

             print(" 輸入字串 :{}  Done".format(string))

         # 3 :等待

         elif cmd_type == 3.0:

             wait_time = sheet.cell_value(i, 2)

             time.sleep(wait_time)

             print(" 等待 {} 秒   Done".format(wait_time))

         # 4 :鍵盤熱鍵

         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 + 1

# 任務二:蹲點等人退課

def 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))


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2847951/,如需轉載,請註明出處,否則將追究法律責任。

相關文章