python自動化神器pyautogui使用步驟
一、pyautogui是什么?
pyautogui是一個Python模塊,可以模擬用戶在屏幕上的鼠標和鍵盤操作。它可以自動化鼠標和鍵盤輸入,可以用于各種自動化任務,例如GUI測試、自動化數(shù)據(jù)輸入、自動化游戲玩法等。pyautogui提供了一組函數(shù)來控制鼠標和鍵盤,例如移動鼠標、單擊、雙擊、右鍵單擊、按下和釋放鍵等。它還提供了一些額外的功能,例如捕捉屏幕截圖、識別顏色和圖像等,以及其他一些實用工具,例如獲取屏幕尺寸和鼠標位置。
二、使用步驟
1.安裝和引入庫
pip install pyautogui
import pyautogui
2.基本操作
(1)鼠標控制
PyAutoGUI可以模擬鼠標的點擊和移動。以下是一些基本操作: moveTo(x, y):將鼠標移動到屏幕上的指定位置。 click(x=None, y=None, button='left'):在指定位置單擊鼠標左鍵、右鍵或中鍵。 doubleClick(x=None, y=None, button='left'):在指定位置雙擊鼠標左鍵、右鍵或中鍵。 rightClick(x=None, y=None):在指定位置單擊鼠標右鍵。 middleClick(x=None, y=None):在指定位置單擊鼠標中鍵。 dragTo(x, y, duration=0.5):將鼠標拖動到指定位置。'
實例:
import pyautogui # 將鼠標移動到屏幕中央 pyautogui.moveTo(pyautogui.size()[0]/2, pyautogui.size()[1]/2) # 在屏幕中央單擊鼠標左鍵 pyautogui.click()
(2)鍵盤控制
PyAutoGUI還可以模擬鍵盤的輸入。以下是一些基本操作:
typewrite(message, interval=0.1):將字符串輸入到鍵盤,可以設置鍵入每個字符的時間間隔。
press(key):按下指定的鍵。
release(key):釋放指定的鍵。
hotekey('ctrl',key)::按下組合鍵以下是一個例子,演示如何將“Hello, world!”字符串鍵入到計算機上:
import pyautogui
# 將“Hello, world!”字符串鍵入計算機
pyautogui.typewrite('Hello, world!')
# 模擬按下鍵盤的A鍵
pyautogui.press('a')
# 模擬釋放鍵盤的A鍵
pyautogui.release('a')
#組合鍵
pyautogui.hotkey('ctrl','v')(3)屏幕截圖
PyAutoGUI可以截取屏幕上的圖像。以下是一個基本操作:
screenshot():截取屏幕上的圖像,并返回PIL圖像對象。
以下是一個例子,演示如何截取整個屏幕的圖像:
import pyautogui # 截取整個屏幕 screenshot = pyautogui.screenshot() # 顯示截圖 screenshot.show()
也可以截取指定位置尺寸的圖片
imag=pyautogui.screenshot(region=(0, 0, 300, 400))#(x,y,w,e)4個點的位置
imag.save('1.png')#保存位置(4)圖片位置識別
PyAutoGUI可以識別圖片所在的位置
img_path='location.png' location=pyautogui.locateOnScreen(img_path) print(location)
但是很多時候圖片識別不到,返回None,這個時候就要對識別參數(shù)進行設置
confidence 是一個可選參數(shù),表示搜索圖像時所需的置信度或準確度。它是一個介于0到1之間的浮點數(shù),表示函數(shù)在搜索圖像時所需的匹配準確度。值越高,匹配準確度就越高,但搜索速度可能會變慢。值越低,則匹配準確度可能會降低,但搜索速度會更快。
例如,當設置confidence為0.5時,函數(shù)將會搜索與給定圖像相匹配的區(qū)域,并且只有當置信度大于等于0.5時,函數(shù)才會返回該區(qū)域的位置。因此,confidence的值可以影響函數(shù)的性能和準確性,取決于您所需要的搜索結果的精度和速度。
pyautogui.locateOnScreen(confidence=0.5)
(5) 獲取鼠標位置
import pyautogui
# 獲取鼠標的當前位置
x, y = pyautogui.position()
print(f"鼠標當前位置:{x}, {y}")也可以獲取圖片上鼠標的位置
import pyautogui
import time
def get_mouse_postion():
time.sleep(5)
print('開始獲取鼠標位置')
time.sleep(1)
x, y = pyautogui.position()
postion = '鼠標坐標帶你({},{})'.format(str(x).rjust(4), str(y).rjust(4))
pix = pyautogui.screenshot().getpixel((x, y)) # 獲取鼠標所在屏幕點的RGB顏色
postion += 'RGB:(' + str(pix[0]).rjust(3) + ',' + str(pix[1]).rjust(3) + ',' + str(pix[2]).rjust(3) + ')'
print(postion)
pyautogui.click(x, y)
print(x,y)
with open('坐標.csv','a',encoding='utf-8')as f:
f.write(str(x))
f.write(',')
f.write(str(y))
f.write('\n')
print('結束')
get_mouse_postion()(6)其他
保護措施:
python移動鼠標、點擊鍵盤非??欤赡軙斐善渌赡軉栴},為了及時中斷,PyAutoGUI提供了一個保護措施。當pyautogui.FAILSAFE = True時,把鼠標光標在屏幕左上角,PyAutoGUI函數(shù)就會產生pyautogui.FailSafeException異常,中斷程序。如果想禁用這個特性,把FAILSAFE設置成False:
import pyautogui pyautogui.FAILSAFE = False
時間延遲
pyautogui.PAUSE 設置延遲,提供頁面反映時間,避免頁面還沒架加載好久執(zhí)行
import pyautogui pyautogui.PAUSE = 2.5
一、模塊需要
- pyautogui
- pyperclip
pip install pyautogui pip install pyperclip
pyautogu上篇文章已經詳細說明了,但是由于輸入格式問題,只能輸入英文,所以pyperclip 將文本內容復制到粘貼板,然后用pyautogu進行鍵盤粘貼操作
txt='I love you'
pyperclip.copy(txt)
pyautogui.hotkey('ctrl','v')三、自動發(fā)消息給對象
1.截圖操作
分別截取微信PC的圖像,以及微信聊天框中的搜索框圖片,如下


盡量截圖小點,分別保存成1.png,2.png
2.python代碼
設置配置
pyautogui.PAUSE=1#每次延遲1秒 pyautogui.FAILSAFE=True wechat_id='jiejieluoguo'#你對象的微信賬號
返回主界面
pyautogui.hotkey('win', 'm')獲取微信圖標位置并點擊
#獲取微信圖標位置,并點擊
location1=pyautogui.locateOnScreen('1.png', confidence=0.7)
print(location1)
pyautogui.doubleClick(location1)獲取搜索框位置,單擊輸入賬號,回車,到聊天界面
location2=pyautogui.locateOnScreen('2.png', confidence=0.7)
print(location2)
pyautogui.doubleClick(pyautogui.center(location1))
pyautogui.typewrite(wechat_id)#寫入微信賬號
pyautogui.press('enter')#回車創(chuàng)建一個名為語料的文本,存入你想說的話(可以上網搜一搜相關語錄)

讀取語錄中的內容
with open('語錄','r',encoding='utf-8')as f:
lists=f.readlines()然后循環(huán)粘貼回車發(fā)送
for i in lists:
i=i.strip()
pyperclip.copy(i)#復制到剪切板
pyautogui.hotkey('ctrl','v')#粘貼到輸入框,回車
pyautogui.press('enter')完整代碼``
import pyautogui
import pyperclip
pyautogui.PAUSE=1#每次延遲1秒
pyautogui.FAILSAFE=True
wechat_id='jiejieluoguo'#你女朋友微信賬號
pyautogui.hotkey('win', 'm')
#獲取微信圖標位置,并點擊
location1=pyautogui.locateOnScreen('1.png', confidence=0.7)
print(location1)
pyautogui.doubleClick(location1)
location2=pyautogui.locateOnScreen('2.png', confidence=0.7)
print(location2)
pyautogui.doubleClick(location2)
pyautogui.typewrite(wechat_id)#寫入微信賬號
pyautogui.press('enter')#回車
with open('語錄','r',encoding='utf-8')as f:
lists=f.readlines()
for i in lists:
i=i.strip()
pyperclip.copy(i)#復制到剪切板
pyautogui.hotkey('ctrl','v')#粘貼到輸入框,回車
pyautogui.press('enter')總結
需要更多了解關于pyautogui知識,可以訪問官址https://pyautogui.readthedocs.io/en/latest/
到此這篇關于python自動化神器pyautogui使用步驟的文章就介紹到這了,更多相關python自動化神器pyautogui內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
django-crontab實現(xiàn)服務端的定時任務的示例代碼
這篇文章主要介紹了django-crontab實現(xiàn)服務端的定時任務的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-02-02

