python第三方包系列:pyautogui

pyautogui 是一款模拟鼠标键盘操作的自动化库,可完成自动化操作。


import pyautogui


# 所有键盘按键
pyautogui.KEYBOARD_KEYS
pyautogui.KEY_NAMES

# 所有ascii可打印字符
pyautogui.QWERTY
pyautogui.QWERTZ


# 获取屏幕宽度/高度
sw, sh = pyautogui.size()
# 当前位置
cx, cy = pyautogui.position()
# (x, y) 是否在屏幕上
pyautogui.onScreen(100, 500)

# 移动到屏幕正中间
pyautogui.moveTo(sw/2, sh/2, duration=0.0)
# 移动到当前位置的相对位置
pyautogui.moveRel(xo=None, yo=None, duration=0.0)

# 鼠标拖拽
pyautogui.dragTo(100, 200, button='left')
pyautogui.dragTo(300, 400, duration=2, button='left')
pyautogui.dragRel(xo=None, yo=None, duration=0.1, button='left')

# 缓动/渐变 tween/easing
#  开始很慢,不断加速
pyautogui.moveTo(100, 100, 2, pyautogui.easeInQuad)
#  开始很快,不断减速
pyautogui.moveTo(100, 100, 2, pyautogui.easeOutQuad)
#  开始和结束都快,中间比较慢
pyautogui.moveTo(100, 100, 2, pyautogui.easeInOutQuad)
#  一步一徘徊前进
pyautogui.moveTo(100, 100, 2, pyautogui.easeInBounce)
#  徘徊幅度更大,甚至超过起点和终点
pyautogui.moveTo(100, 100, 2, pyautogui.easeInElastic)


# 单击/右击/中击/双击/三击
pyautogui.click(x=moveToX, y=moveToY, clicks=1, interval=0.1, duration=2, button='left')
pyautogui.rightClick(x=moveToX, y=moveToY)
pyautogui.middleClick(x=moveToX, y=moveToY)
pyautogui.doubleClick(x=moveToX, y=moveToY)
pyautogui.tripleClick(x=moveToX, y=moveToY)

# 滚动/按下/松开
pyautogui.scroll(clicks=10, x=moveToX, y=moveToY)
pyautogui.mouseDown(x=moveToX, y=moveToY, button='left')
pyautogui.mouseUp(x=moveToX, y=moveToY, button='left')


# 键盘操作
pyautogui.typewrite('hello', interval=0.25)
pyautogui.typewrite(['a', 'b', 'c', 'enter'], interval=0.1)
pyautogui.press('esc')
pyautogui.keyDown('shift')
pyautogui.press(['left', 'lfet', 'left', 'left'])
pyautogui.keyUp('shift')

# 热键操作
pyautogui.hotkey('ctrl', 'c')
pyautogui.hotkey('alt', 'ctrl', 'del')

# 保护措施, 鼠标移到左上角, 触发异常
pyautogui.FAILSAFE = True

# 为所有函数增加延迟
pyautogui.PAUSE = 2.5


# 消息弹窗
pyautogui.alert(text='msg', title='', button='OK')
pyautogui.confirm(text='msg', title='', button=['OK', 'Cancel'])
pyautogui.confirm(text='msg', title='', buttons=range(10))
pyautogui.prompt(text='msg', title='' , default='')
pyautogui.password(text='', title='', default='', mask='*')


# 截屏, 返回 pillow.Image 对象
im1 = pyautogui.screenshot()
im2 = pyautogui.screenshot('demo.png')
im3 = pyautogui.screenshot(region=(0, 0, 300, 400))

# 定位函数
loc = pyautogui.locateOnScreen('pyautogui/looks.png')
x1, y1 = pyautogui.center(loc)
pyautogui.locateAllOnScreen('pyautogui/looks.png')
x, y = pyautogui.locateCenterOnScreen('pyautogui/looks.png')
# 滑块验证码,可用, 定位needleImage在haystackImage的坐标
pyautogui.locateAll(needleImage, haystackImage)

# 像素匹配
im = pyautogui.screenshot()
im.getpixel((100, 200)) # 获取截图指定坐标的像素值
pyautogui.pixel(100, 200)  # 直接获取屏幕坐标像素
# 指定坐标像素是否匹配特定像素
pyautogui.pixelMatchesColor(100, 200, (255, 255, 255))
pyautogui.pixelMatchesColor(100, 200, (255, 255, 245), tolerance=10)


# 光标位置记录
try:
    while True:
        x, y = pyautogui.position()
        positionStr = 'X: {} Y: {}'.format(*[str(x).rjust(4) for x in [x, y]])
        print(positionStr, end='')
        print('\b' * len(positionStr), end='', flush=True)
except KeyboardInterrupt:
    print('\n')