python pyauto模块详解

发布时间:2022-06-08 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了python pyauto模块详解脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

# 获取当前鼠标位置print(pyautogui.position())在每次调用PyAutoGUI的函数后设置2.5秒的暂停:

# 暂停2.5spyautogui.PAUSE = 2.5

此外,为了防止程序出问题,当鼠标移动到屏幕左上角,会引发pyautogui.FailSafeException错误进而中止程序。关闭命令如下(不建议关闭):

pyautogui.FAILSAFE = False 鼠标移动

# 用num_seconds(秒)将鼠标移动到(x,y)位置

x = 200y = 100

num_seconds = 1

pyautogui.moveTo(x, y, duration=num_seconds)

# 用num_seconds(秒)将鼠标从当前位置向右移动xOffset,向下移动yOffset

# 如果duration为0或未指定,则立即移动。

xOffset = 30

yOffset = -50

num_seconds = 0.5

pyautogui.moveRel(xOffset, yOffset, duration=num_seconds)

鼠标拖动# 用num_seconds(秒)将鼠标推动到(x,y)位置# 鼠标拖动是指按下鼠标左键移动鼠标。x = 200y = 100num_seconds= 1pyautogui.dragTo(x, y, duration=num_seconds)

# 用num_seconds(秒)将鼠标从当前位置向右拖动xOffset,向下推动yOffsetxOffset = 30yOffset = -50num_seconds = 0.5pyautogui.dragRel(xOffset, yOffset, duration=num_seconds) 

鼠标单击# 将鼠标移动到(moveToX,moveToY)位置,点击鼠标num_of_clicks次,每次点击间隔secs_between_clicks秒# button表示单击方式,'left'左键单击,'middle'中键单击,'right'右键单击moveToX = 500moveToY = 600num_of_clicks = 1secs_between_clicks = 1pyautogui.click(x=moveToX, y=moveToY, clicks=num_of_clicks, interval=secs_between_clicks, button='left')所有的鼠标点击都可以用click()完成,但也存在一些函数是为了方便阅读,如下所示。

moveToX = 10moveToY = 20# 右键单击pyautogui.rightClick(x=moveToX + 50, y=moveToY)# 中键单击pyautogui.middleClick(x=moveToX + 50, y=moveToY)# 左键双击pyautogui.doubleClick(x=moveToX + 50, y=moveToY)# 左键三击pyautogui.tripleClick(x=moveToX + 50, y=moveToY)4.4 鼠标滚动moveToX = 100moveToY = 200# 鼠标在当前位置向下滑动100格# pyautogui.scroll(clicks=-100)# 鼠标移动到(moveToX,moveToY)位置,然后向上滚动150格pyautogui.scroll(clicks=150, x=moveToX, y=moveToY)4.5 鼠标按下# 鼠标移动到(moveToX,moveToY)位置,鼠标左键按下pyautogui.mouseDown(x=moveToX, y=moveToY, button='left')# 鼠标移动到(moveToX,moveToY)位置,鼠标右键松开(按下右键的情况下)pyautogui.mouseUp(x=moveToX, y=moveToY, button='right')# 鼠标在当前位置,按下中键pyautogui.mouseDown(button='middle')4.6 缓动/渐变(Tween / Easing)函数缓动/渐变函数的作用是让光标的移动更炫。如果你不需要用到的话,你可以忽略这些。PyAutoGUI有30种缓动/渐变函数,可以通过以下函数查看

print(pyautogui.ease*?)常用缓动/渐变函数使用示例如下:

moveToX = 100moveToY = 100# #开始慢,结束快pyautogui.moveTo(moveToX + 5 , moveToY+ 45, 2, pyautogui.easeInQuad) # 开始快,结束慢 pyautogui.moveTo(moveToX + 15, moveToY+ 35, 2, pyautogui.easeOutQuad) # 快速开始和结束,中间缓慢pyautogui.moveTo(moveToX + 25, moveToY+ 25, 2, pyautogui.easeInOutQuad) # 最后反弹pyautogui.moveTo(moveToX + 35, moveToY+ 15, 2, pyautogui.easeInBounce) # 反复横跳pyautogui.moveTo(moveToX + 45, moveToY+ 5, 2, pyautogui.easeInElastic) 5 键盘函数5.1 文字输入键盘控制文字输入的主要函数就是typewrite()/write()。这个函数可以实现字符输入,可以用interval参数设置两次输入间时间间隔。

# 在当前位置输入文字text,每个字符输入间隔secs_between_keys秒# n表示换行text = 'Hello world!n'secs_between_keys = 0.1pyautogui.typewrite(message=text, interval=secs_between_keys) # 在当前位置按下键盘各种键pyautogui.typewrite(['t', 'a', 'b', 'c', 'left', 'backspace', 'enter', 'f1','n'], interval=secs_between_keys)# 查看所有支持按键print(pyautogui.KEYBOARD_KEYS)5.2 快捷键通过keyDown/keyUp按下或者松开键盘,通过hotkey执行快捷键操作。

# ctrl+c 复制文字pyautogui.hotkey('ctrl', 'c') # ctrl+v 粘贴文字pyautogui.hotkey('ctrl', 'v')

# 按下ctrl键pyautogui.keyDown('ctrl')# 按下v键,相当文字粘贴pyautogui.keyDown('v')# 松开ctrl键盘pyautogui.keyUp('ctrl')当然可以使用press()函数设置按下某个键再释放某个键,如下所示。

# 按下shift键pyautogui.keyDown('shift')pyautogui.press('left')pyautogui.press('left')pyautogui.press('left')# 松开shift键pyautogui.keyUp('shift')同时也可以和typewrite()函数一样,用数组把一组键传入press(),或者设置press按压次数。

# 按下三个left键pyautogui.press(['left', 'left', 'left'])# 按left键五次pyautogui.press('left', presses=5)5.3 hold()上下文管理器hold()函数可以用作上下文管理器,并从pyautogui.KEYBOARD_KEYS传递一个字符串,并且该键将在上下文块的持续时间内保持。示例如下:

# 按住shiftwith pyautogui.hold('shift'): # 连续按left,然后松开shift pyautogui.press(['left', 'left', 'left'])

# 上面代码功能和下面代码实现功能相同# 按下shift键pyautogui.keyDown('shift')pyautogui.press('left')pyautogui.press('left')pyautogui.press('left')# 松开shift键pyautogui.keyUp('shift')6 消息框函数如果你需要暂停程序直到用户点击确定,或者想向用户显示一些信息,可以使用消息框函数。这里消息框函数的使用方式和javascript一样。

# 警告窗口alert_result = pyautogui.alert('点击确定返回字符串OK')# 确认窗口confirm_result = pyautogui.confirm('点击确定返回字符串OK,点击取消返回字符串Cancel')# 点击ok保存输入的文字,点击Cancel返回Noneprompt_result = pyautogui.prompt('输入文字')# 点击ok保存输入的密码,点击Cancel返回None# default默认文字,mask用什么符号代替输入的密码password_result = pyautogui.password(text='', title='', default='', mask='*')7 截图函数PyAutoGUI使用Pillow/PIL库实现图像的处理。在Linux上,您必须运行以下命令安装scrot库才能使用屏幕截图功能。

sudo apt-get install scrot7.1 截屏# 截屏返回result对象result = pyautogui.screenshot()# result是PIL中的Image对象print(type(result))# 保存图像result.save('result1.jpg')# 展示图片#result.show()

# imageFilename参数设置文件保存为止,在截屏前保存图片到本地foo.png文件# region设置截图区域[x,y,w,h],以(x,y)为左上角顶点,截宽w,高h的区域result = pyautogui.screenshot(imageFilename='result2.jpg',region=[10,20,100,50])

7.2 图像定位PyAutoGUI提供了多个定位函数。都是从左上角原点开始向右向下搜索截图位置。具体如下:

locateOnScreen(image, grayscale=False):在屏幕中,返回和image图片最类似区域的坐标(left, top, width, height),如果没找到返回None。grayscale设置是否灰度查找。locateCenterOnScreen(image, grayscale=False):在屏幕中,返回和image图片最类似区域的中心坐标(x, y),如果没找到返回None。locateAllOnScreen(image, grayscale=False):在屏幕中,返回和image图片所有类似区域的坐标(left, top, width, height)的生成器locate(needleImage, haystackImage, grayscale=False):在haystackImage中,返回和image图片最类似区域的坐标(left, top, width, height)。locateAll(needleImage, haystackImage, grayscale=False):在haystackImage中,返回和image图片所有类似区域的坐标(left, top, width, height)的生成器。官方说在1920x1080屏幕上,screenshot()函数大约需要100毫秒。但实测图像定位需要花费3秒左右,而且常常找不到图片相似区域。可选的confidence关键字参数指定函数在屏幕上定位图像的准确性。如果由于像素差异可忽略不计,函数无法定位图像,调低confidence将提高查找命中结果。但是需要安装OpenCV才能使confidence关键字工作。

图像定位函数基础使用如下:

# 在屏幕返回和result1.jpg图片类似的区域坐标,返回值(左上角x坐标,左上角y坐标,宽度,高度)# 如果没找到返回Noneresult = pyautogui.locateOnScreen('result1.jpg')# 在屏幕返回和result1.jpg图片类似的区域中间位置的XY坐标,confidence返回区域最低置信度result = pyautogui.locateCenterOnScreen('result1.jpg', confidence=0.9)# 为查找图片找到的所有位置返回一个生成器results = pyautogui.locateAllOnScreen('result1.jpg', confidence=0.6)print(results)# 打印各组的(左上角x坐标,左上角y坐标,宽度,高度)for i in results: print(i)# 将结果保存为listlist_result = list(pyautogui.locateAllOnScreen('result1.jpg', confidence=0.6)

# 在haystackImage中,返回和image图片最类似区域的坐标result = pyautogui.locate(needleImage='result1.jpg', haystackImage='result.jpg', confidence=0.5)# 在haystackImage中,返回和image图片所有类似区域的坐标(left, top, width, height)result = pyautogui.locateAll(needleImage='result1.jpg', haystackImage='result.jpg', confidence=0.5)这些“定位”功能相当昂贵;他们可能需要整整几秒钟的时间才能运行。加速它们的最好方法是传递一个region参数(一个(左、上、宽、高)的4整数元组)来只搜索屏幕的较小区域而不是全屏。但是这个region区域必须比待搜索截图区域大,否则会引发错误。代码如下:

result = pyautogui.locateOnScreen('result1.jpg', region=(0,0, 300, 400))result = pyautogui.locate(needleImage='result1.jpg', haystackImage='result.jpg', confidence=0.5, region=(0,0, 300, 400))您可以传递grayscale=True给定位函数以提供轻微的加速(大约30%左右)。这会降低图像和屏幕截图的颜色饱和度,加快定位速度,但可能会导致误报匹配。

result_location = pyautogui.locateOnScreen('result.jpg', grayscale=True,confidence=0.6)此外要获取截屏某个位置的RGB像素值,可以用PIL中Image对象的getpixel()方法,也可以用PyAutoGUI的pixel()函数。

im = pyautogui.screenshot()print(im.getpixel((100, 200)))print(pyautogui.pixel(100, 200))如果您只需要验证单个像素是否与给定像素匹配,请调用该pixelMatchesColor()函数,并将其表示的颜色的X坐标、Y坐标和RGB元组传递给它:

# 颜色匹配pyautogui.pixelMatchesColor(100, 200, (255, 255, 255))# tolerance参数可以指定红、绿、蓝3种颜色误差范围pyautogui.pixelMatchesColor(100, 200, (248, 250, 245), tolerance=10)

搜索

复制

脚本宝典总结

以上是脚本宝典为你收集整理的python pyauto模块详解全部内容,希望文章能够帮你解决python pyauto模块详解所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: