借助pyautogui庫,我們可以輕松地控制滑鼠、鍵盤以及進行影像識別,實作自動搶課的功能
1.準備作業
我們在倉庫里提供了2個必須的檔案,包括:
- auto_get_lesson_pic_recognize.py:腳本檔案
- info.xlsx:執行操作資訊檔案
在運行這個腳本(
auto_get_lesson_pic_recognize.py)前,你需要:
很多人學習蟒蛇,不知道從何學起,
很多人學習python,掌握了基本語法之后,不知道在哪里尋找案例上手,
很多已經做了案例的人,卻不知道如何去學習更多高深的知識,
那么針對這三類人,我給大家提供一個好的學習平臺,免費獲取視頻教程,電子書,以及課程的源代碼!
QQ群:101677771
歡迎加入,一起討論一起學習!
1.安裝python并成功配置環境變數,可以在cmd下這樣檢查;若回傳版本號,則已安裝
python --version
2.安裝以下的依賴,windows用戶請以管理員用戶運行cmd并依次執行:
# pyautogui庫
pip install pyautogui
# 讀取excel表格的庫
pip install xlrd==1.2.0
# 向計算機的剪貼板發送文本/從計算機剪貼板接收文本的庫
pip install pyperclip
# 計算機視覺庫
pip install opencv-python
# 影像處理庫
pip install pillow
到此,成功安裝了5個庫
2.配合使用py腳本和xlsx檔案
第一步
需要將搶課的每一步所需要點擊的圖示/超鏈接在頭腦中想清楚
第二步
將搶課每一步的所需點擊的圖示/超鏈接截圖,保存在和py腳本同一路徑下
打開excel表格,根據第一行提示在單元格中進行輸入:
- A列------備注(可填可不填)
- B列------操作型別,目前包括:
- 1.左鍵單擊(回圈直到找到圖片為止):意思就是如果沒有找到你設定的那張圖片,它就一直找下去,找不到就不停;你所設定的次數是找到成功的次數
- 2.輸入字串
- 3.等待
- 4.熱鍵
- 5.左鍵單擊(無需找到圖片):找圖片不管找沒找到,就找那這么多次,次數=找到成功的次數+找到失敗的次數
- C列------B列的引數
- 待點擊圖示名(包括圖片后綴名,如.png)
- 等待的時間(秒)
- 輸入的字串
- 熱鍵
- D列------單擊重復次數
- 不填,默認為1
- 若想無限單擊,填-1
按照你的選課步驟從第2行開始順序填寫excel表格的執行步驟
此時,保存excel表格
第三步
我們打開需要進行操作的選課網頁
我們在cmd下切換到腳本所在目錄
# 切換到D盤
D:
# 切換到xx檔案夾
cd xx
python auto_get_lesson_pic_recognize.py
根據提示執行即可
上圖示例
3.auto_get_lesson_pic_recognize功能介紹
(1).搶課一次
注意
- 截圖時請隨機應變,匹配到影像后,滑鼠自動點擊影像正中央,建議配合qq截圖,ctrl+a/t+a,選取一個獨一無二的標記在截圖中并且將所要點擊的點放在qq截圖四個藍點的中央
- 如果遇到同一畫面中需要點擊的圖示存在多個一樣的,沒有特征參照物,可以在那一步設定等待若干秒,手動點擊圖示
- 若未成功識別圖片,將回圈執行識別操作;手動點擊圖示成功,excel表格中中的指令也會跳到下一條
- 考慮到網路延遲問題,建議合理利用等待功能
(2).蹲點撿漏
- 在搶課一次的基礎上套了一層死回圈
- 巧妙利用f5、左鍵單擊(回圈直到找到圖片為止)、左鍵單擊(無需找到圖片),可以24h掛機實作蹲點撿漏
- 請發揮你的聰明才智,正確截圖
4.坐標版本(不建議使用)
坐標版本位于coordinate_version目錄下
如果能夠確切知道所點擊的位置的坐標,可以選用坐標版本
配合qq截圖,你能夠輕松知道你的滑鼠在1920×1080解析度下在螢屏上的坐標(以像素為單位)
順序排列單擊位置的坐標,實作搶課
excel表格中根據提示填寫坐標、操作
5.代碼
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 = 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))
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/388915.html
標籤:Python
下一篇:python畫圣誕樹「全網最全」
