开源AI浏览器自动化框架!14.3K+ Star,自然语言+代码,爬虫效率翻倍!

 

传统浏览器自动化工具如Selenium、Playwright易受UI变化影响,维护成本高,而全AI代理缺乏可控性。

Stagehand 由Browserbase开发,是一款开源AI浏览器自动化框架,基于Playwright,结合自然语言和代码控制,集成OpenAI/Anthropic模型,提供预览、缓存和结构化数据提取。

通过act、extract、observe三个核心API,将自然语言指令与传统代码结合,支持复杂任务自动化和数据提取,兼容Browserbase云端浏览器。

目前 Stagehand 获14.3K+ GitHub Star,以其混合编程模式和生产级稳定性受到开发者追捧。

核心功能

  • • 混合编程:自然语言驱动AI操作,Playwright执行精确控制。
  • • 多模型支持:兼容OpenAI(GPT-4o)、Anthropic(Claude 3.7 Sonnet)、Gemini等。
  • • 预览与缓存:observe预览AI动作,缓存重复操作节省成本。
  • • 结构化提取:基于Zod/Pydantic schema提取网页数据。

安装指南

支持前端Node.js环境快速安装,以下是安装指令:

npx create-browser-app

也可以通过源代码构建和运行

git clone https://github.com/browserbase/stagehand.git
cd stagehand
pnpm install
pnpm playwright install
pnpm run build
pnpm run example # run the blank script at ./examples/example.ts
pnpm run example 2048 # run the 2048 example at ./examples/2048.ts

设置AI模型API,需要复制一个环境变量文件。

cp .env.example .env
nano .env # Edit the .env file to add API keys

使用调用方式

// Use Playwright functions on the page object
const page = stagehand.page;
await page.goto("https://github.com/browserbase");

// Use act() to execute individual actions
await page.act("click on the stagehand repo");

// Use Computer Use agents for larger actions
const agent = stagehand.agent({
    provider: "openai",
    model: "computer-use-preview",
});
await agent.execute("Get to the latest PR");

// Use extract() to read data from the page
const { author, title } = await page.extract({
  instruction: "extract the author and title of the PR",
  schema: z.object({
    author: z.string().describe("The username of the PR author"),
    title: z.string().describe("The title of the PR"),
  }),
});

写在最后

Stagehand 以AI+Playwright为核心,结合自然语言和代码控制,简化浏览器自动化和数据提取,Browserbase云端支持,提升生产效率。

不论是做网页爬虫、自动化测试,还是做表单填充、自动更新文档、多任务并行提取,都不失为一个可靠的选择。

特别推荐给需要做网页测试、数据抓取、自动操作界面流程的 AI 工程师、爬虫开发者、SaaS 创业者。

GitHub 项目地址:https://github.com/browserbase/stagehand







(文:开源星探)

发表评论