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
相关推荐
我的运维人生6 分钟前
Python技术深度探索:从基础到进阶的实践之旅(第一篇)
开发语言·python·运维开发·技术共享
Bonne journée7 分钟前
‌在Python中,print(f‘‘)是什么?
java·开发语言·python
iiimZoey32 分钟前
Dit架构 diffusion范式分类+应用
人工智能·python
R三哥哥啊33 分钟前
【树莓派5B】IO串口通信使用
python
互联网时光机1 小时前
python写的window小工具-一键设置ip自动获取ip
网络·windows·python
SCBAiotAigc1 小时前
VSCode debug模式无法跳转进入内置模块
人工智能·vscode·python
call_me_wangcheng1 小时前
django创建项目
python·django
VXbishe1 小时前
(附源码)基于springboot的“我来找房”微信小程序的设计与实现-计算机毕设 23157
java·python·微信小程序·node.js·c#·php·课程设计
chusheng18401 小时前
Python 如何使用 multiprocessing 模块创建进程池
python·信息可视化·matplotlib
猿小猴子1 小时前
Python3 爬虫 中间人爬虫
开发语言·爬虫·python