从0到1构建类人自动化工具:基于Ghost Cursor的项目实战案例

【免费下载链接】ghost-cursor 🖱️ Generate human-like mouse movements with puppeteer or on any 2D plane 【免费下载链接】ghost-cursor 项目地址: https://gitcode.com/gh_mirrors/gh/ghost-cursor

Ghost Cursor是一款强大的类人鼠标移动生成工具,能够模拟人类的鼠标移动轨迹,帮助开发者构建更自然的自动化脚本。无论是网页自动化测试还是模拟用户行为,Ghost Cursor都能让你的程序表现得像真人操作一样,有效避免被检测为机器人。

为什么选择Ghost Cursor?

在自动化脚本开发中,机械、僵硬的鼠标移动很容易被识别为机器人行为。而Ghost Cursor通过模拟人类自然的鼠标移动模式,解决了这一痛点。它基于贝塞尔曲线生成平滑的移动路径,并结合Fitts定律计算移动速度,使鼠标移动看起来更加真实自然。

核心优势

  • 类人移动轨迹:采用贝塞尔曲线生成自然的鼠标路径,避免机械感
  • 智能速度调整:根据距离和目标大小自动调整移动速度
  • Puppeteer集成:与Puppeteer无缝集成,简化自动化脚本开发
  • 随机化行为:模拟人类操作中的微小偏差和犹豫,增强真实性

快速开始:安装与基础使用

环境准备

在开始之前,请确保你的开发环境中已经安装了Node.js和npm/yarn。然后通过以下命令安装Ghost Cursor:

yarn add ghost-cursor

或者使用npm:

npm install ghost-cursor

基础示例:生成鼠标移动路径

下面是一个简单的示例,展示如何生成两点之间的类人鼠标移动路径:

import { path } from "ghost-cursor"

const from = { x: 100, y: 100 }
const to = { x: 600, y: 700 }

const route = path(from, to)

这段代码将生成一个包含多个坐标点的数组,模拟从(100,100)到(600,700)的自然鼠标移动路径。

与Puppeteer集成:构建真实浏览器自动化

Ghost Cursor与Puppeteer的集成非常简单,下面是一个完整的示例,展示如何使用Ghost Cursor控制Puppeteer进行页面操作:

import { GhostCursor } from "ghost-cursor"
import puppeteer from "puppeteer"

const run = async (url) => {
  const selector = "#sign-up button"
  const browser = await puppeteer.launch({ headless: false });
  const page = await browser.newPage()
  const cursor = new GhostCursor(page)
  await page.goto(url)
  await page.waitForSelector(selector)
  await cursor.click(selector)
}

在这个示例中,我们创建了一个GhostCursor实例,并使用它来点击页面上的按钮。GhostCursor会自动处理鼠标移动、悬停和点击的全过程,模拟人类的操作方式。

Puppeteer特定行为

Ghost Cursor为Puppeteer提供了一些特殊优化:

  • 当目标元素距离较远时,会自动模拟"过头"然后调整的行为
  • 移动到元素时,会随机选择元素内的一个点,而不是精确中心
  • 鼠标速度会根据距离和目标元素大小自动调整

高级功能与自定义选项

方法详解

Ghost Cursor提供了丰富的方法来满足不同的自动化需求:

new GhostCursor(page, options)

创建Ghost Cursor实例,参数包括:

  • page: Puppeteer页面实例
  • start: 初始光标位置,默认为{ x: 0, y: 0 }
  • performRandomMoves: 是否执行随机移动,默认为false
  • visible: 是否显示光标,默认为false
click(selector, options)

模拟点击操作,支持多种选项如延迟、点击次数等。

move(selector, options)

移动鼠标到指定元素,支持调整移动速度、延迟等参数。

路径生成原理

Ghost Cursor的核心是生成自然的鼠标移动路径。它使用贝塞尔曲线创建两点之间的平滑路径,并通过随机生成控制点来模拟人类移动的不确定性。同时,它还应用Fitts定律来计算移动速度,使移动更加符合人类行为特征。

实战案例:构建类人登录自动化

下面我们将通过一个实际案例,展示如何使用Ghost Cursor构建一个类人登录自动化脚本:

import { GhostCursor } from "ghost-cursor"
import puppeteer from "puppeteer"

async function humanLikeLogin(url, username, password) {
  const browser = await puppeteer.launch({ headless: false });
  const page = await browser.newPage();
  const cursor = new GhostCursor(page, { visible: true });
  
  try {
    await page.goto(url);
    
    // 移动到用户名输入框并点击
    await cursor.move('#username');
    await cursor.click();
    await page.type('#username', username, { delay: 100 + Math.random() * 200 });
    
    // 随机延迟
    await new Promise(resolve => setTimeout(resolve, 500 + Math.random() * 1000));
    
    // 移动到密码输入框并点击
    await cursor.move('#password');
    await cursor.click();
    await page.type('#password', password, { delay: 100 + Math.random() * 200 });
    
    // 随机延迟
    await new Promise(resolve => setTimeout(resolve, 300 + Math.random() * 800));
    
    // 点击登录按钮
    await cursor.move('#login-button');
    await cursor.click();
    
    // 等待登录完成
    await page.waitForNavigation();
    console.log('登录成功');
  } catch (error) {
    console.error('登录失败:', error);
  } finally {
    // 保持浏览器打开一段时间
    await new Promise(resolve => setTimeout(resolve, 5000));
    await browser.close();
  }
}

// 使用示例
humanLikeLogin('https://example.com/login', 'your_username', 'your_password');

这个案例展示了如何结合Ghost Cursor和Puppeteer创建一个高度模拟人类行为的登录脚本,包括随机延迟、自然的鼠标移动和键盘输入速度变化。

总结与扩展

Ghost Cursor为自动化脚本开发提供了强大的类人鼠标移动能力,使你的程序能够更自然地与网页交互。通过结合Puppeteer等工具,你可以构建出几乎无法与人类操作区分的自动化脚本。

进一步学习资源

无论你是开发网页自动化测试、构建爬虫,还是创建用户行为模拟工具,Ghost Cursor都能为你的项目增添一层真实感和可靠性。现在就开始尝试,体验类人自动化的强大能力吧!

安装与贡献

要开始使用Ghost Cursor,只需通过npm或yarn安装即可。如果你对项目有兴趣,可以通过以下方式获取完整代码:

git clone https://gitcode.com/gh_mirrors/gh/ghost-cursor

欢迎贡献代码、报告问题或提出建议,一起完善这个强大的类人鼠标移动工具!

【免费下载链接】ghost-cursor 🖱️ Generate human-like mouse movements with puppeteer or on any 2D plane 【免费下载链接】ghost-cursor 项目地址: https://gitcode.com/gh_mirrors/gh/ghost-cursor

Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐