python自動腳本的pyautogui入門學(xué)習(xí)
本文介紹了pyautogui入門學(xué)習(xí),分享給大家,也給自己留個筆記
安裝
pip install pyautogui
學(xué)習(xí)使用
加載模塊
import pyautogui
獲取信息類
電腦屏幕的左上角是位置(0,0),向右X坐標(biāo)增加,向下Y坐標(biāo)增加
# 獲取屏幕尺寸screenWidth, screenHeight = pyautogui.size()# 獲取鼠標(biāo)當(dāng)前位置currentMouseX, currentMouseY = pyautogui.position()
進(jìn)行操作類
鼠標(biāo)
# 鼠標(biāo)移動到屏幕中心pyautogui.moveTo(screenWidth / 2, screenHeight / 2)# 將鼠標(biāo)移動到固定位置pyautogui.moveTo(100, 100)# 用“1秒”的時間移動到固定位置pyautogui.moveTo(100, 100, duration=1)# 將鼠標(biāo)移動到相對當(dāng)前“下方100”的位置pyautogui.moveRel(0, 100) # 鼠標(biāo)左鍵拖拽到屏幕“100,200”的位置pyautogui.dragTo(100, 200, button=’left’) # 鼠標(biāo)左鍵用“2秒”拖拽到屏幕“300,400”的位置pyautogui.dragTo(300, 400, 2, button=’left’) # 鼠標(biāo)左鍵用“2秒”拖拽到相對當(dāng)前“右邊30”的位置pyautogui.dragRel(30, 0, 2, button=’left’)### button屬性可以有:leftmiddleright# 鼠標(biāo)左鍵單擊pyautogui.click()# 鼠標(biāo)左鍵單擊“100,200”位置pyautogui.click(x=100, y=200)# 鼠標(biāo)左鍵雙擊pyautogui.doubleClick()# 鼠標(biāo)左鍵三擊pyautogui.tripleClick()# 鼠標(biāo)右鍵單擊pyautogui.click(button=’right’)# 鼠標(biāo)右鍵單擊pyautogui.rightClick()# 鼠標(biāo)右鍵間隔0.25秒單擊3次pyautogui.click(button=’right’, clicks=3, interval=0.25)# 鼠標(biāo)左鍵放下pyautogui.mouseDown()# 鼠標(biāo)左鍵抬起pyautogui.mouseUp()# 鼠標(biāo)滾輪上滾10pyautogui.scroll(10)# 鼠標(biāo)滾輪下滾10pyautogui.scroll(-10)
鍵盤
# 輸入一個字符串pyautogui.typewrite(’Hello world!’)# 每個字母間隔0.25秒輸入一個字符串pyautogui.typewrite(’Hello world!’, interval=0.25) # 鍵入 'a', 然后敲擊左方向鍵, 再鍵入 'b'.pyautogui.typewrite([’a’,’left’,’b’])# 按鍵pyautogui.press(’esc’)# 連續(xù)按鍵pyautogui.press([’left’, ’left’, ’left’, ’left’, ’left’, ’left’])# 鍵盤按下pyautogui.keyDown(’shift’)# 鍵盤釋放pyautogui.keyUp(’shift’)# 熱鍵按住ctrl的同時按下cpyautogui.hotkey(’ctrl’, ’c’)
支持的按鍵有:
[’t’, ‘n’, ‘r’, ’ ‘, ‘!’, ‘'’, ‘#’, ‘$’, ‘%’, ‘&’, '’', ‘(’,‘)’, ‘*’, ‘+’, ‘,’, ‘-’, ‘.’, ‘/’, ‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’,‘8’, ‘9’, ‘:’, ‘;’, ‘<’, ‘=’, ‘>’, ‘?’, ‘@’, ‘[’, ‘’, ‘]’, ‘^’, ‘_’, ‘`’,‘a(chǎn)’, ‘b’, ‘c’, ‘d’, ‘e’,‘f’, ‘g’, ‘h’, ‘i’, ‘j’, ‘k’, ‘l’, ‘m’, ‘n’, ‘o’,‘p’, ‘q’, ‘r’, ‘s’, ‘t’, ‘u’, ‘v’, ‘w’, ‘x’, ‘y’, ‘z’, ‘{’, ‘|’, ‘}’, ‘~’,‘a(chǎn)ccept’, ‘a(chǎn)dd’, ‘a(chǎn)lt’, ‘a(chǎn)ltleft’, ‘a(chǎn)ltright’, ‘a(chǎn)pps’, ‘backspace’,‘browserback’, ‘browserfavorites’, ‘browserforward’, ‘browserhome’,‘browserrefresh’, ‘browsersearch’, ‘browserstop’, ‘capslock’, ‘clear’,‘convert’, ‘ctrl’, ‘ctrlleft’, ‘ctrlright’, ‘decimal’, ‘del’, ‘delete’,‘divide’, ‘down’, ‘end’, ‘enter’, ‘esc’, ‘escape’, ‘execute’, ‘f1’, ‘f10’,‘f11’, ‘f12’, ‘f13’, ‘f14’, ‘f15’, ‘f16’, ‘f17’, ‘f18’, ‘f19’, ‘f2’, ‘f20’,‘f21’, ‘f22’, ‘f23’, ‘f24’, ‘f3’, ‘f4’, ‘f5’, ‘f6’, ‘f7’, ‘f8’, ‘f9’,‘final’, ‘fn’, ‘hanguel’, ‘hangul’, ‘hanja’, ‘help’, ‘home’, ‘insert’, ‘junja’,‘kana’, ‘kanji’, ‘launchapp1’, ‘launchapp2’, ‘launchmail’,‘launchmediaselect’, ‘left’, ‘modechange’, ‘multiply’, ‘nexttrack’,‘nonconvert’, ‘num0’, ‘num1’, ‘num2’, ‘num3’, ‘num4’, ‘num5’, ‘num6’,‘num7’, ‘num8’, ‘num9’, ‘numlock’, ‘pagedown’, ‘pageup’, ‘pause’, ‘pgdn’,‘pgup’, ‘playpause’, ‘prevtrack’, ‘print’, ‘printscreen’, ‘prntscrn’,‘prtsc’, ‘prtscr’, ‘return’, ‘right’, ‘scrolllock’, ‘select’, ‘separator’,‘shift’, ‘shiftleft’, ‘shiftright’, ‘sleep’, ‘space’, ‘stop’, ‘subtract’, ‘tab’,‘up’, ‘volumedown’, ‘volumemute’, ‘volumeup’, ‘win’, ‘winleft’, ‘winright’, ‘yen’,‘command’, ‘option’, ‘optionleft’, ‘optionright’]
消息盒子
pyautogui.alert(text=’你好嗎’, title=’問候’, button=’我很好’)
返回button值
pyautogui.confirm(text=’你好嗎’, title=’問候’, buttons=[’我很好’, ’我不好’, ’不告訴你’])
返回輸入值
pyautogui.prompt(text=’你好嗎’, title=’問候’ , default=’’)
將輸入值用*號隱藏,返回輸入值
pyautogui.password(text=’’, title=’’, default=’’, mask=’*’)
截圖功能
首先需要安裝Pillow模塊
im1 = pyautogui.screenshot()# 截圖整個屏幕并命名保存到本地im2 = pyautogui.screenshot(’my_screenshot.png’)# 截圖區(qū)域“左側(cè),頂部,寬度和高度”im = pyautogui.screenshot(region=(0, 0, 300, 400))# 返回值(left, top, width, height)button7location = pyautogui.locateOnScreen(’calc7key.png’)
實(shí)戰(zhàn)練習(xí)
1、計算機(jī)自動計算
打開電腦自帶的計算器利用截圖工具截取4張圖,并依次命名將python程序和計算器一同打開在桌面上運(yùn)行python程序
# 自動計算器輸入import pyautoguix = [0]*4x[0] = pyautogui.locateCenterOnScreen(’7.png’)x[1] = pyautogui.locateCenterOnScreen(’+.png’)x[2] = pyautogui.locateCenterOnScreen(’5.png’)x[3] = pyautogui.locateCenterOnScreen(’=.png’)for i in range(4): pyautogui.click(x[i])
2、自動畫圖
# 自動畫圖import pyautoguiimport timetime.sleep(5)distance = 200while distance > 0: pyautogui.dragRel(distance, 0, duration=0.5) # move right distance -= 5 pyautogui.dragRel(0, distance, duration=0.5) # move down pyautogui.dragRel(-distance, 0, duration=0.5) # move left distance -= 5 pyautogui.dragRel(0, -distance, duration=0.5) # move up
參見官網(wǎng):https://pyautogui.readthedocs.io/en/latest/index.html
到此這篇關(guān)于python自動腳本的pyautogui入門學(xué)習(xí)的文章就介紹到這了,更多相關(guān)pyautogui入門 內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. 如何在jsp界面中插入圖片2. ASP實(shí)現(xiàn)加法驗證碼3. python selenium 獲取接口數(shù)據(jù)的實(shí)現(xiàn)4. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)5. 詳解JSP 內(nèi)置對象request常見用法6. 利用ajax+php實(shí)現(xiàn)商品價格計算7. Python matplotlib 繪制雙Y軸曲線圖的示例代碼8. jsp EL表達(dá)式詳解9. JSP servlet實(shí)現(xiàn)文件上傳下載和刪除10. springboot集成與使用Sentinel的方法
