詳情可訪問:pyautogui官網(wǎng)地址,關(guān)注本專欄,學(xué)習(xí)自動(dòng)發(fā)消息給對象
一、pyautogui是什么?
pyautogui是一個(gè)Python模塊,可以模擬用戶在屏幕上的鼠標(biāo)和鍵盤操作。它可以自動(dòng)化鼠標(biāo)和鍵盤輸入,可以用于各種自動(dòng)化任務(wù),例如GUI測試、自動(dòng)化數(shù)據(jù)輸入、自動(dòng)化游戲玩法等。pyautogui提供了一組函數(shù)來控制鼠標(biāo)和鍵盤,例如移動(dòng)鼠標(biāo)、單擊、雙擊、右鍵單擊、按下和釋放鍵等。它還提供了一些額外的功能,例如捕捉屏幕截圖、識別顏色和圖像等,以及其他一些實(shí)用工具,例如獲取屏幕尺寸和鼠標(biāo)位置。
二、使用步驟
1.安裝和引入庫
pip install pyautogui
import pyautogui
2.基本操作
(1)鼠標(biāo)控制
PyAutoGUI可以模擬鼠標(biāo)的點(diǎn)擊和移動(dòng)。以下是一些基本操作:
moveTo(x, y):將鼠標(biāo)移動(dòng)到屏幕上的指定位置。
click(x=None, y=None, button='left'):在指定位置單擊鼠標(biāo)左鍵、右鍵或中鍵。
doubleClick(x=None, y=None, button='left'):在指定位置雙擊鼠標(biāo)左鍵、右鍵或中鍵。
rightClick(x=None, y=None):在指定位置單擊鼠標(biāo)右鍵。
middleClick(x=None, y=None):在指定位置單擊鼠標(biāo)中鍵。
dragTo(x, y, duration=0.5):將鼠標(biāo)拖動(dòng)到指定位置。'
實(shí)例:
import pyautogui
# 將鼠標(biāo)移動(dòng)到屏幕中央
pyautogui.moveTo(pyautogui.size()[0]/2, pyautogui.size()[1]/2)
# 在屏幕中央單擊鼠標(biāo)左鍵
pyautogui.click()
(2)鍵盤控制
PyAutoGUI還可以模擬鍵盤的輸入。以下是一些基本操作:
typewrite(message, interval=0.1):將字符串輸入到鍵盤,可以設(shè)置鍵入每個(gè)字符的時(shí)間間隔。
press(key):按下指定的鍵。
release(key):釋放指定的鍵。
hotekey('ctrl',key)::按下組合鍵
以下是一個(gè)例子,演示如何將“Hello, world!”字符串鍵入到計(jì)算機(jī)上:
import pyautogui
# 將“Hello, world!”字符串鍵入計(jì)算機(jī)
pyautogui.typewrite('Hello, world!')
# 模擬按下鍵盤的A鍵
pyautogui.press('a')
# 模擬釋放鍵盤的A鍵
pyautogui.release('a')
#組合鍵
pyautogui.hotkey('ctrl','v')
(3)屏幕截圖
PyAutoGUI可以截取屏幕上的圖像。以下是一個(gè)基本操作:
screenshot():截取屏幕上的圖像,并返回PIL圖像對象。
以下是一個(gè)例子,演示如何截取整個(gè)屏幕的圖像:
import pyautogui
# 截取整個(gè)屏幕
screenshot = pyautogui.screenshot()
# 顯示截圖
screenshot.show()
也可以截取指定位置尺寸的圖片
imag=pyautogui.screenshot(region=(0, 0, 300, 400))#(x,y,w,e)4個(gè)點(diǎn)的位置
imag.save('1.png')#保存位置
(4)圖片位置識別
PyAutoGUI可以識別圖片所在的位置
img_path='location.png'
location=pyautogui.locateOnScreen(img_path)
print(location)
但是很多時(shí)候圖片識別不到,返回None,這個(gè)時(shí)候就要對識別參數(shù)進(jìn)行設(shè)置
confidence 是一個(gè)可選參數(shù),表示搜索圖像時(shí)所需的置信度或準(zhǔn)確度。它是一個(gè)介于0到1之間的浮點(diǎn)數(shù),表示函數(shù)在搜索圖像時(shí)所需的匹配準(zhǔn)確度。值越高,匹配準(zhǔn)確度就越高,但搜索速度可能會變慢。值越低,則匹配準(zhǔn)確度可能會降低,但搜索速度會更快。
例如,當(dāng)設(shè)置confidence為0.5時(shí),函數(shù)將會搜索與給定圖像相匹配的區(qū)域,并且只有當(dāng)置信度大于等于0.5時(shí),函數(shù)才會返回該區(qū)域的位置。因此,confidence的值可以影響函數(shù)的性能和準(zhǔn)確性,取決于您所需要的搜索結(jié)果的精度和速度。
pyautogui.locateOnScreen(confidence=0.5)
(6) 獲取鼠標(biāo)位置
import pyautogui
# 獲取鼠標(biāo)的當(dāng)前位置
x, y = pyautogui.position()
print(f"鼠標(biāo)當(dāng)前位置:{x}, {y}")
也可以獲取圖片上鼠標(biāo)的位置
import pyautogui
import time
def get_mouse_postion():
time.sleep(5)
print('開始獲取鼠標(biāo)位置')
time.sleep(1)
x, y = pyautogui.position()
postion = '鼠標(biāo)坐標(biāo)帶你({},{})'.format(str(x).rjust(4), str(y).rjust(4))
pix = pyautogui.screenshot().getpixel((x, y)) # 獲取鼠標(biāo)所在屏幕點(diǎn)的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('坐標(biāo).csv','a',encoding='utf-8')as f:
f.write(str(x))
f.write(',')
f.write(str(y))
f.write('\n')
print('結(jié)束')
get_mouse_postion()
(7)其他
保護(hù)措施:
python移動(dòng)鼠標(biāo)、點(diǎn)擊鍵盤非??欤赡軙斐善渌赡軉栴},為了及時(shí)中斷,PyAutoGUI提供了一個(gè)保護(hù)措施。當(dāng)pyautogui.FAILSAFE = True時(shí),把鼠標(biāo)光標(biāo)在屏幕左上角,PyAutoGUI函數(shù)就會產(chǎn)生pyautogui.FailSafeException異常,中斷程序。如果想禁用這個(gè)特性,把FAILSAFE設(shè)置成False:
import pyautogui
pyautogui.FAILSAFE = False
時(shí)間延遲
pyautogui.PAUSE 設(shè)置延遲,提供頁面反映時(shí)間,避免頁面還沒架加載好久執(zhí)行
import pyautogui
pyautogui.PAUSE = 2.5
一、模塊需要
- pyautogui
- pyperclip
pip install pyautogui
pip install pyperclip
pyautogu上篇文章已經(jīng)詳細(xì)說明了,但是由于輸入格式問題,只能輸入英文,所以pyperclip 將文本內(nèi)容復(fù)制到粘貼板,然后用pyautogu進(jìn)行鍵盤粘貼操作
txt='I love you'
pyperclip.copy(txt)
pyautogui.hotkey('ctrl','v')
三、自動(dòng)發(fā)消息給對象
1.截圖操作
分別截取微信PC的圖像,以及微信聊天框中的搜索框圖片,如下
盡量截圖小點(diǎn),分別保存成1.png,2.png
2.python代碼
設(shè)置配置
pyautogui.PAUSE=1#每次延遲1秒
pyautogui.FAILSAFE=True
wechat_id='jiejieluoguo'#你對象的微信賬號
返回主界面
pyautogui.hotkey('win', 'm')
獲取微信圖標(biāo)位置并點(diǎn)擊
#獲取微信圖標(biāo)位置,并點(diǎn)擊
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)建一個(gè)名為語料的文本,存入你想說的話(可以上網(wǎng)搜一搜相關(guān)語錄)
讀取語錄中的內(nèi)容
with open('語錄','r',encoding='utf-8')as f:
lists=f.readlines()
然后循環(huán)粘貼回車發(fā)送
for i in lists:
i=i.strip()
pyperclip.copy(i)#復(fù)制到剪切板
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')
#獲取微信圖標(biāo)位置,并點(diǎn)擊
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)#復(fù)制到剪切板
pyautogui.hotkey('ctrl','v')#粘貼到輸入框,回車
pyautogui.press('enter')
總結(jié)
需要更多了解關(guān)于pyautogui知識,可以訪問官址https://pyautogui.readthedocs.io/en/latest/文章來源:http://www.zghlxwxcb.cn/news/detail-537544.html
希望大家多多支持,一起努力學(xué)習(xí),后續(xù)慢慢分享更多新奇有趣的東西文章來源地址http://www.zghlxwxcb.cn/news/detail-537544.html
到了這里,關(guān)于python自動(dòng)化神器:pyautogui的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!