Python中如何用py模拟人类鼠标轨迹?

比如给定 2 个坐标点 point_ = [(11,222),(77,333)]
生成一个人类鼠标的轨迹

有没有类似的生成库,或者大佬有什么思路呢)



\n
补充一点,
js 的 window.event 事件中有鼠标监控
Python中如何用py模拟人类鼠标轨迹?

4 回复

PyAutoGUI


import pyautogui
import random
import math
import time

class HumanMouse:
    def __init__(self):
        self.current_pos = pyautogui.position()
        
    def _bezier_curve(self, start, end, control_points, steps=100):
        """生成贝塞尔曲线路径点"""
        points = []
        for i in range(steps + 1):
            t = i / steps
            # 二次贝塞尔曲线公式
            x = (1-t)**2 * start[0] + 2*(1-t)*t * control_points[0][0] + t**2 * end[0]
            y = (1-t)**2 * start[1] + 2*(1-t)*t * control_points[0][1] + t**2 * end[1]
            points.append((int(x), int(y)))
        return points
    
    def _add_human_noise(self, points):
        """添加人类抖动和速度变化"""
        noisy_points = []
        for i, (x, y) in enumerate(points):
            # 在曲线中间部分添加轻微抖动
            if 0.3 < i/len(points) < 0.7:
                x += random.randint(-2, 2)
                y += random.randint(-2, 2)
            noisy_points.append((x, y))
        return noisy_points
    
    def _human_speed_profile(self, total_points):
        """生成人类速度曲线(慢-快-慢)"""
        speeds = []
        for i in range(total_points):
            t = i / (total_points - 1)
            # 正弦速度曲线,中间快两头慢
            speed = math.sin(t * math.pi) * 0.8 + 0.2
            speeds.append(max(0.01, speed))
        return speeds
    
    def move_to(self, target_x, target_y, duration=1.0):
        """模拟人类鼠标移动到目标位置"""
        start_x, start_y = self.current_pos
        
        # 生成控制点(稍微偏离直线)
        mid_x = (start_x + target_x) / 2 + random.randint(-50, 50)
        mid_y = (start_y + target_y) / 2 + random.randint(-50, 50)
        
        # 生成贝塞尔曲线路径
        path = self._bezier_curve(
            (start_x, start_y),
            (target_x, target_y),
            [(mid_x, mid_y)]
        )
        
        # 添加人类特征
        path = self._add_human_noise(path)
        speeds = self._human_speed_profile(len(path))
        
        # 计算每步的时间间隔
        total_time = duration
        time_per_point = total_time / len(path)
        
        # 执行移动
        for i, (x, y) in enumerate(path):
            pyautogui.moveTo(x, y, _pause=False)
            time.sleep(time_per_point * speeds[i])
        
        self.current_pos = (target_x, target_y)
    
    def click(self, target_x=None, target_y=None, duration=0.5):
        """模拟人类点击(先移动再点击)"""
        if target_x is not None and target_y is not None:
            self.move_to(target_x, target_y, duration)
            time.sleep(random.uniform(0.1, 0.3))  # 点击前短暂停顿
        
        # 添加点击时的微小移动
        x, y = pyautogui.position()
        pyautogui.moveTo(x + random.randint(-1, 1), 
                        y + random.randint(-1, 1), 
                        duration=0.01)
        pyautogui.click()

# 使用示例
if __name__ == "__main__":
    mouse = HumanMouse()
    
    # 移动到屏幕中心并点击
    screen_width, screen_height = pyautogui.size()
    mouse.move_to(screen_width // 2, screen_height // 2, duration=1.5)
    mouse.click()
    
    # 随机移动几次
    for _ in range(3):
        x = random.randint(100, screen_width - 100)
        y = random.randint(100, screen_height - 100)
        mouse.move_to(x, y, duration=random.uniform(0.8, 1.5))
        mouse.click()

核心思路就是用贝塞尔曲线代替直线移动,加上速度变化和随机抖动。贝塞尔曲线能让移动轨迹更自然,速度曲线模拟人类"慢-快-慢"的移动习惯,随机抖动增加真实感。记得先装pyautogui库。

简单说就是曲线移动加随机扰动。

我试一下哈

selenium,pyppeteer 都可以试试

回到顶部