Python操作鼠标键盘和爬虫

一.pyautogui 库

pyautogui 是一个 Python 库,允许控制鼠标和键盘。可以通过它编写 Python 脚本来自动执行各种任务,例如点击按钮、输入文本、移动鼠标等。这个库非常适合用来编写自动化脚本来完成重复性的工作,比如网页表单填写、屏幕截图、GUI测试或者爬虫等。

base 复制代码
pip install pyautogui

二.常用功能

1. 获取屏幕大小

python 复制代码
import pyautogui

# 获取屏幕尺寸
screen_width, screen_height = pyautogui.size()
print("屏幕尺寸: %d x %s" % (screen_width, screen_height))

2.移动鼠标到屏幕中央

python 复制代码
# 移动鼠标到屏幕中央
center_x, center_y = screen_width // 2, screen_height // 2
# duration 是移动时间, 默认为0, 这里在一秒内移动到中间
pyautogui.moveTo(center_x, center_y, duration=1)

3.单击鼠标

python 复制代码
# 指定位置单击鼠标
pyautogui.click(button_x, button_y)

4.右键点击鼠标

python 复制代码
pyautogui.click(button='right')

5.双击鼠标

python 复制代码
# 指定位置双击鼠标
pyautogui.doubleClick(button_x, button_y)

6.键盘输入

python 复制代码
# 输入文本
pyautogui.typewrite("Hello, world!")

7.按下按键

python 复制代码
# 按下 enter 键
pyautogui.press('enter')

8.按下组合键

python 复制代码
### 按下组合键 Ctrl+S
pyautogui.hotkey('ctrl', 's')

9.截取全屏并保存

python 复制代码
# 截取全屏并保存
screenshot = pyautogui.screenshot()
screenshot.save("screenshot.png")

10.截取部分屏幕

python 复制代码
# 截取部分屏幕
region = (0, 0, 300, 400)  # x, y, width, height
screenshot = pyautogui.screenshot(region=region)
screenshot.save("screenshot_region.png")

三.根据与图像寻找位置并双击

1. 寻找屏幕中的 button.png 的位置, 并双击

python 复制代码
import pyautogui
import time

time.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 的包

base 复制代码
pip install opencv-python
python 复制代码
import pyautogui
import time

time.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 参数, 在指定区域内寻找

python 复制代码
# 定义屏幕区域 (x, y, width, height)
region = (100, 100, 800, 600)

button_location = pyautogui.locateOnScreen('button.png', region=region, confidence=0.8)

四.爬虫

我们常见的爬虫, 有分析网页的, 有分析后端接口的, 其实还有这种简单粗暴, 直接操作鼠标键盘进行重复性保存的, 比如有某个网站有一个图片组成的电子书, 需要点击按钮到下一页, 如果手动保存的需要一个个另存为, 但是因为下一页的位置是固定的, 每页图片出现的位置是固定的, 所以直接可以使用 pyautogui 代替我们进行手动的翻页和另存为。

1. 寻找按钮的位置

我们可以按照上面说的, 根据按钮的截图自动取寻找按钮的坐标, 但是精度不能保证, 或者相似的图标太多都是问题。

我们也可以先读出按钮的坐标, 如下所示, 在5秒内将鼠标移动到按钮位置, 然后等待打印就可以获取了

python 复制代码
import pyautogui
import time

print("请在5秒内将鼠标移动到按钮位置...")
time.sleep(5)
button_position = pyautogui.position()

print(f"按钮位置: {button_position}")

2.爬虫

主要是进行另存为和翻页的功能

python 复制代码
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 // 2
    pyautogui.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' % cnt
    pyautogui.typewrite(file_path)
    # 等待一秒键入成功
    time.sleep(1)
    # 这个按钮是将输入法中的文字键入地址框
    pyautogui.press('enter')
    
    # 这个按钮是指执行文件另存为时的保存按钮
    pyautogui.press('enter')

    # 等待保存和下载完成
    time.sleep(2)

    cnt += 1
相关推荐
2601_9679801110 小时前
Flask Web 应用
python·flask·web开发·环境变量·虚拟环境
aifannihao11 小时前
ß 大写变 SS:用 Python 算清 Ruby2Shell 的缓冲区账本
python·安全·标准库·缓冲区溢出·字符串编码
liwulin050611 小时前
【OLLAMA】调用SKILL的通用ReAct
python
郝学胜-神的一滴12 小时前
Effective Python 条款36:优雅拼装迭代器与生成器
开发语言·数据结构·人工智能·python·numpy·scikit-learn
Norna13 小时前
Git 给路径加了引号,这个文件就从 review 里消失了
python
wuyk55513 小时前
Python办公自动化-第04讲-批量新建Excel
开发语言·python·excel
wuyk55513 小时前
Spider-第20讲-综合实战可上线爬虫
大数据·爬虫·python·elasticsearch
JonaHu14 小时前
关键词排名突然消失?用 Python、SQLite 和 SERP API 搭一套可解释的 SEO 监控
python·sqlite·seo·serp api
prince_zxill15 小时前
把 17 个散文件装进一个文件夹:拆解 claude-plugins-official 的插件打包术(47,413 Star 实测)
python·github·agent·mcp·claude code
少司府15 小时前
嵌入式外设篇(三)GPIO·三:MCU系统结构、总线与统一编制
linux·python·stm32·单片机·嵌入式硬件·mcu·嵌入式软件