Python PyAutoGUI 库

在爬虫的时候有些元素可能无法点击或者被获取。

在这个时候我们需要考虑使用 PyAutoGUI 库,这个库被用来对鼠和键盘来进行操作。

可以考察下下面的代码:

import time

import pyautogui

while True:
    # 移动鼠标,duration=0.1是鼠标移动过程中的延迟速度
    pyautogui.moveTo(x=300, y=300, duration=0.1)
    time.sleep(3)

    # 移动鼠标到坐标后,单击左键
    pyautogui.click(x=700, y=300, duration=0.1)
    time.sleep(3)

    # 移动鼠标到坐标后,双击左键
    pyautogui.doubleClick(x=600, y=300, duration=0.1)
    time.sleep(3)

    # 移动鼠标到坐标后,单击右键
    pyautogui.rightClick(x=700, y=300, duration=0.1)
    time.sleep(3)

x , y 上面为屏幕中的像素地址。

有关 PyAutoGUI 的使用情况和用例,请访问其官方地址:Welcome to PyAutoGUI’s documentation! — PyAutoGUI documentation

python-gui-01

上面画出图形就是使用 PyAutoGUI 库生成的。

比如说是使用鼠标在画图工具中进行画图生成的图形。