一.pyautogui 庫
pyautogui 是一個 Python 庫,允許控制鼠標和鍵盤。可以通過它編寫 Python 腳本來自動執行各種任務,例如點擊按鈕、輸入文本、移動鼠標等。這個庫非常適合用來編寫自動化腳本來完成重復性的工作,比如網頁表單填寫、屏幕截圖、GUI測試或者爬蟲等。
pip install pyautogui
二.常用功能
1. 獲取屏幕大小
import pyautogui# 獲取屏幕尺寸
screen_width, screen_height = pyautogui.size()
print("屏幕尺寸: %d x %s" % (screen_width, screen_height))
2.移動鼠標到屏幕中央
# 移動鼠標到屏幕中央
center_x, center_y = screen_width // 2, screen_height // 2
# duration 是移動時間, 默認為0, 這里在一秒內移動到中間
pyautogui.moveTo(center_x, center_y, duration=1)
3.單擊鼠標
# 指定位置單擊鼠標
pyautogui.click(button_x, button_y)
4.右鍵點擊鼠標
pyautogui.click(button='right')
5.雙擊鼠標
# 指定位置雙擊鼠標
pyautogui.doubleClick(button_x, button_y)
6.鍵盤輸入
# 輸入文本
pyautogui.typewrite("Hello, world!")
7.按下按鍵
# 按下 enter 鍵
pyautogui.press('enter')
8.按下組合鍵
### 按下組合鍵 Ctrl+S
pyautogui.hotkey('ctrl', 's')
9.截取全屏并保存
# 截取全屏并保存
screenshot = pyautogui.screenshot()
screenshot.save("screenshot.png")
10.截取部分屏幕
# 截取部分屏幕
region = (0, 0, 300, 400) # x, y, width, height
screenshot = pyautogui.screenshot(region=region)
screenshot.save("screenshot_region.png")
三.根據與圖像尋找位置并雙擊
1. 尋找屏幕中的 button.png 的位置, 并雙擊
import pyautogui
import timetime.sleep(5)# 查找圖像在屏幕上的位置
button_location = pyautogui.locateOnScreen('button.png')if button_location:# 獲取圖像的中心位置button_x, button_y = pyautogui.center(button_location)# 點擊圖像中心pyautogui.doubleClick(button_x, button_y)
else:print("按鈕未找到")
2.對于找不到圖片的問題
有時候圖像的分辨率不合適, 或者匹配度不高, 可以增加 locateOnScreen() 的 confidence 參數為匹配度, 當匹配度高于指定值, 則匹配位置但是需要安裝 OpenCV 的包
pip install opencv-python
import pyautogui
import timetime.sleep(5)# 查找圖像在屏幕上的位置
button_location = pyautogui.locateOnScreen('button.png', confidence=0.8)if button_location:# 獲取圖像的中心位置button_x, button_y = pyautogui.center(button_location)# 點擊圖像中心pyautogui.doubleClick(button_x, button_y)
else:print("按鈕未找到")
3.指定區域搜索圖像
locateOnScreen() 還可以增加 region 參數, 在指定區域內尋找
# 定義屏幕區域 (x, y, width, height)
region = (100, 100, 800, 600)button_location = pyautogui.locateOnScreen('button.png', region=region, confidence=0.8)
四.爬蟲
我們常見的爬蟲, 有分析網頁的, 有分析后端接口的, 其實還有這種簡單粗暴, 直接操作鼠標鍵盤進行重復性保存的, 比如有某個網站有一個圖片組成的電子書, 需要點擊按鈕到下一頁, 如果手動保存的需要一個個另存為, 但是因為下一頁的位置是固定的, 每頁圖片出現的位置是固定的, 所以直接可以使用 pyautogui 代替我們進行手動的翻頁和另存為。
1. 尋找按鈕的位置
我們可以按照上面說的, 根據按鈕的截圖自動取尋找按鈕的坐標, 但是精度不能保證, 或者相似的圖標太多都是問題。
我們也可以先讀出按鈕的坐標, 如下所示, 在5秒內將鼠標移動到按鈕位置, 然后等待打印就可以獲取了
import pyautogui
import timeprint("請在5秒內將鼠標移動到按鈕位置...")
time.sleep(5)
button_position = pyautogui.position()print(f"按鈕位置: {button_position}")
2.爬蟲
主要是進行另存為和翻頁的功能
import pyautogui
import time# 設置按鈕的位置 (x, y), 就是下一頁的位置
button_x, button_y = 1896, 598# 等待操作完成,比如新頁面加載或彈出框出現
time.sleep(3)cnt = 1
# 387 為操作次數, 比如一個
while cnt < 387:# 點擊按鈕, 進行翻頁if cnt > 1:pyautogui.click(button_x, button_y)# 等待翻頁成功time.sleep(1)# 將鼠標移動到屏幕中間 (假設圖片在屏幕中間)screen_width, screen_height = pyautogui.size()center_x, center_y = screen_width // 2, screen_height // 2pyautogui.moveTo(center_x, center_y)# 右鍵點擊以彈出菜單pyautogui.click(button='right')# 等待菜單出現time.sleep(1)# 模擬按鍵操作(假設另存為是上下箭頭后回車)# 需要調整按鍵序列以匹配實際情況# 下箭頭(因為另存為按鈕在第二的位置)pyautogui.press('down') pyautogui.press('down')# 回車pyautogui.press('enter')# 等待另存為窗口出現time.sleep(2)# 模擬按鍵操作以輸入文件名并保存file_path = '%d.jpg' % cntpyautogui.typewrite(file_path)# 等待一秒鍵入成功time.sleep(1)# 這個按鈕是將輸入法中的文字鍵入地址框pyautogui.press('enter')# 這個按鈕是指執行文件另存為時的保存按鈕pyautogui.press('enter')# 等待保存和下載完成time.sleep(2)cnt += 1