📊 项目概览

基本信息

项目 详情
项目名称 browser-use
GitHub https://github.com/browser-use/browser-use
Stars 87.8k+
Forks 10.1k+
License MIT
版本 0.5.2+ (持续更新)
创始人 Magnus Muller, Gregor Zunic
官网 https://browser-use.com
文档 https://docs.browser-use.com

项目定位

Browser-Use 是让 AI 代理控制浏览器的最简单方式

通过 LLM 赋能,使 AI 代理能够像人类一样浏览网页、点击按钮、填写表单,并自主完成复杂的 Web 任务。


🎯 核心价值主张

解决什么问题?

  1. API 缺失: 许多网站不提供 API,但需要自动化访问
  2. 动态内容: 传统爬虫难以处理 JavaScript 渲染的内容
  3. 交互复杂: 需要登录、验证码、多步骤操作
  4. AI 集成: 让 LLM 能够"看到"和"操作"网页

核心优势

特性 说明
LLM 驱动 使用大语言模型理解网页和做出决策
视觉识别 可以"看到"网页元素并定位
自然语言 用自然语言描述任务即可执行
开源框架 完全开源,可自定义和扩展
云服务 提供托管服务,含隐身浏览器、代理、验证码解决

🏗️ 技术架构

核心组件

┌─────────────────────────────────────────────────────────┐
│                    Browser-Use 架构                      │
├─────────────────────────────────────────────────────────┤
│                                                         │
│  ┌─────────────┐    ┌─────────────┐    ┌─────────────┐ │
│  │  感知层     │    │  决策层     │    │  执行层     │ │
│  │  (Vision)   │ →  │  (LLM)      │ →  │  (Action)   │ │
│  │             │    │             │    │             │ │
│  │ • DOM 解析   │    │ • 任务理解   │    │ • 点击      │ │
│  │ • 元素识别   │    │ • 路径规划   │    │ • 输入      │ │
│  │ • 截图分析   │    │ • 错误处理   │    │ • 导航      │ │
│  └─────────────┘    └─────────────┘    └─────────────┘ │
│         ↑                                       ↓       │
│         └───────────  反馈循环  ────────────────┘       │
│                                                         │
└─────────────────────────────────────────────────────────┘

技术栈

层级 技术
浏览器控制 Playwright
LLM 集成 OpenAI, Claude, Gemini, 自定义模型
视觉系统 DOM 解析 + 截图分析
推理系统 LLM + Prompt Engineering
执行系统 Playwright Actions
记忆管理 Context Window 优化

📦 安装与配置

方法 1:Python SDK 安装

# 安装 browser-use 包
pip install browser-use

# 或者安装最新开发版
pip install git+https://github.com/browser-use/browser-use.git

方法 2:Web-UI 安装(推荐新手)

# 1. 克隆仓库
git clone https://github.com/browser-use/web-ui.git
cd web-ui

# 2. 创建虚拟环境
python -m venv venv
source venv/bin/activate  # Linux/Mac
# 或 venv\Scripts\activate  # Windows

# 3. 安装依赖
pip install -r requirements.txt

# 4. 配置环境变量
cp .env.example .env
# 编辑 .env 文件,填入 API Key

# 5. 运行 Web-UI
python app.py
# 访问 http://127.0.0.1:7788

环境配置

# 获取 API Key(可选,使用云服务时)
# 访问:https://cloud.browser-use.com/new-api-key

# 设置环境变量
export BROWSER_USE_API_KEY="bu_your_api_key_here"

# 如果使用本地 LLM
export OPENAI_API_KEY="sk-..."
# 或
export ANTHROPIC_API_KEY="sk-ant-..."

💻 核心 API 使用

基础用法

import asyncio
from browser_use import Agent

async def main():
    # 创建 Agent
    agent = Agent(
        task="List the top 20 posts on Hacker News today with their points",
        llm="gpt-4o",  # 或 "claude-sonnet-4", "gemini-2.0"
    )
    
    # 执行任务
    result = await agent.run()
    print(result.output)

asyncio.run(main())

高级用法 - 自定义浏览器

from browser_use import Browser, Agent

async def advanced_example():
    # 创建浏览器实例
    browser = Browser(headless=False)  # headless=False 可以看到浏览器
    
    # 创建 Agent
    agent = Agent(
        task="Go to github.com and search for 'browser-use' repository",
        browser=browser,
        llm="claude-sonnet-4",
    )
    
    # 执行
    result = await agent.run()
    
    # 清理
    await browser.close()

使用云服务

from browser_use_sdk.v3 import AsyncBrowserUse
import asyncio

async def cloud_example():
    client = AsyncBrowserUse(
        api_key="bu_your_api_key"  # 从 cloud.browser-use.com 获取
    )
    
    result = await client.run(
        "List the top 20 posts on Hacker News today with their points"
    )
    
    print(result.output)
    print(f"耗时:{result.execution_time}s")
    print(f"成本:${result.cost}")

asyncio.run(cloud_example())

🎬 实际应用案例

案例 1:自动填写表单

"""
任务:自动填写工作申请表格
"""
from browser_use import Agent

async def job_application():
    agent = Agent(
        task="""
        1. 打开 https://company.com/careers/software-engineer
        2. 填写申请表:
           - 姓名:张三
           - 邮箱:zhangsan@email.com
           - 电话:13800138000
           - 上传简历:/path/to/resume.pdf
        3. 回答问题:
           - 为什么想加入我们?因为贵公司在 AI 领域的创新...
           - 期望薪资:面议
        4. 提交申请
        """,
        llm="claude-sonnet-4",
    )
    
    result = await agent.run()
    return result

案例 2:电商购物

"""
任务:自动购买生活用品
"""
from browser_use import Agent

async def grocery_shopping():
    shopping_list = """
    - 牛奶 2 升
    - 鸡蛋 12 个
    - 面包 2 个
    - 苹果 1kg
    - 鸡肉 500g
    """
    
    agent = Agent(
        task=f"""
        在盒马鲜生网站完成以下购物任务:
        1. 访问 https://www.freshippo.com
        2. 登录账号(使用保存的凭证)
        3. 搜索并添加以下商品到购物车:
        {shopping_list}
        4. 选择最优惠的选项
        5. 结算并使用默认地址
        6. 确认订单但不支付(等待人工确认)
        """,
        llm="gpt-4o",
    )
    
    result = await agent.run()
    print(f"购物车总计:{result.output}")

案例 3:数据抓取

"""
任务:抓取 Hacker News 热门帖子
"""
from browser_use import Agent

async def scrape_hn():
    agent = Agent(
        task="""
        1. 打开 https://news.ycombinator.com
        2. 提取前 20 个帖子的以下信息:
           - 标题
           - 分数
           - 作者
           - 评论数
           - 链接
        3. 以 JSON 格式输出
        """,
        llm="claude-sonnet-4",
    )
    
    result = await agent.run()
    
    # 解析 JSON 输出
    import json
    posts = json.loads(result.output)
    
    for post in posts:
        print(f"{post['title']} - {post['points']} points")

案例 4:PC 配件搜索

"""
任务:帮助组装定制 PC
"""
from browser_use import Agent

async def pc_part_picker():
    requirements = """
    - CPU: AMD Ryzen 7 或 Intel i7
    - GPU: RTX 4070 或更好
    - 内存:32GB DDR5
    - 存储:1TB NVMe SSD
    - 预算:10000-15000 元
    """
    
    agent = Agent(
        task=f"""
        在 PCPartPicker 或京东完成以下任务:
        1. 搜索符合以下要求的配件:
        {requirements}
        2. 比较不同品牌的价格和评价
        3. 确保配件兼容性
        4. 生成购物清单,包含:
           - 配件名称
           - 价格
           - 购买链接
           - 兼容性说明
        5. 总价控制在预算内
        """,
        llm="gpt-4o",
    )
    
    result = await agent.run()
    print(result.output)

案例 5:自动化测试

"""
任务:网站功能测试
"""
from browser_use import Agent

async def website_testing():
    agent = Agent(
        task="""
        测试电商网站的用户注册流程:
        1. 打开 https://demo-site.com/register
        2. 填写注册表单:
           - 使用随机生成的邮箱
           - 密码:Test123!@#
        3. 提交并验证:
           - 是否跳转到登录页
           - 是否收到确认邮件
        4. 尝试登录
        5. 验证个人资料页面
        6. 报告任何错误或异常行为
        """,
        llm="claude-sonnet-4",
    )
    
    result = await agent.run()
    
    # 生成测试报告
    print("=== 测试报告 ===")
    print(result.output)

🔧 高级功能

1. 多 Agent 协作

from browser_use import Agent

async def multi_agent_collaboration():
    # 研究 Agent
    researcher = Agent(
        task="研究 2026 年最佳笔记本电脑,找出前 5 名",
        llm="claude-sonnet-4",
    )
    
    # 比价 Agent
    price_checker = Agent(
        task="在亚马逊、京东、天猫比较这 5 款笔记本的价格",
        llm="gpt-4o",
    )
    
    # 执行
    research_result = await researcher.run()
    print("研究结果:", research_result.output)
    
    price_result = await price_checker.run()
    print("比价结果:", price_result.output)

2. 错误处理与重试

from browser_use import Agent, Browser

async def robust_automation():
    browser = Browser(headless=False)
    
    agent = Agent(
        task="登录 GitHub 并提取我的贡献图",
        browser=browser,
        llm="claude-sonnet-4",
        max_retries=3,  # 最大重试次数
        retry_delay=5,  # 重试间隔(秒)
    )
    
    try:
        result = await agent.run()
        print(result.output)
    except Exception as e:
        print(f"任务失败:{e}")
        # 可以记录日志或发送通知
    finally:
        await browser.close()

3. 自定义工具集成

from browser_use import Agent, register_tool

# 注册自定义工具
@register_tool
def save_to_file(content: str, filename: str):
    """将内容保存到文件"""
    with open(filename, 'w') as f:
        f.write(content)
    return f"已保存到 {filename}"

async def with_custom_tool():
    agent = Agent(
        task="抓取 Hacker News 首页并保存为 JSON",
        llm="gpt-4o",
        tools=[save_to_file],  # 使用自定义工具
    )
    
    result = await agent.run()

📊 性能基准测试

LLM 模型对比(Browser-Use 官方基准)

模型 准确率 速度 (任务/小时) 成本 (每任务) 推荐度
bu-ultra (Browser-Use 云) 78.0% ~15 $0.05 ⭐⭐⭐⭐⭐
Claude Sonnet 4 62.0% ~12 $0.08 ⭐⭐⭐⭐
GPT-4o 52.4% ~6 $0.12 ⭐⭐⭐
Gemini 2.0 59.0% ~10 $0.06 ⭐⭐⭐⭐
Claude Opus 4 62.0% ~8 $0.25 ⭐⭐⭐

数据来源:https://github.com/browser-use/benchmark

选择建议

# 最佳性能(推荐)
llm="bu-ultra"  # Browser-Use 云模型

# 最佳性价比
llm="gemini-2.0"  # 速度快,成本低

# 复杂任务
llm="claude-sonnet-4"  # 推理能力强

# 简单任务
llm="gpt-4o-mini"  # 经济实惠

☁️ 云服务 vs 开源版

对比表

特性 开源版 云服务
价格 免费(+LLM 成本) 按使用量付费
浏览器 自托管 托管,隐身模式
代理 自行配置 195+ 国家,内置
验证码 自行解决 自动解决
扩展性 受限于本地资源 无限并发
维护 自行维护 零配置
适用场景 开发、测试 生产环境

使用建议

选择开源版:

  • 学习和发展
  • 内部工具开发
  • 有技术团队维护
  • 成本敏感

选择云服务:

  • 生产环境
  • 需要高可用性
  • 不想管理基础设施
  • 需要绕过反爬虫

🚀 最佳实践

1. Prompt 工程

# ❌ 不好的 Prompt
task = "帮我买东西"

# ✅ 好的 Prompt
task = """
在京东完成以下购物任务:
1. 访问 https://www.jd.com
2. 搜索"无线鼠标"
3. 筛选条件:
   - 价格:100-300 元
   - 品牌:罗技、雷蛇
   - 评分:4.5 星以上
4. 选择性价比最高的 3 款
5. 添加到购物车(不结算)
6. 输出商品清单和总价
"""

2. 错误处理

async def safe_execution():
    agent = Agent(
        task="...",
        max_failures=3,  # 最大失败次数
        save_conversation=True,  # 保存对话历史
    )
    
    try:
        result = await agent.run()
        
        # 验证结果
        if not result.output:
            raise ValueError("未获取到有效结果")
            
        return result
        
    except Exception as e:
        # 记录错误
        print(f"执行失败:{e}")
        
        # 可以尝试降级方案
        # 或者人工介入
        raise

3. 性能优化

# 缓存页面
browser.cache("https://example.com")

# 使用 headless 模式(更快)
browser = Browser(headless=True)

# 限制上下文
agent = Agent(
    task="...",
    max_context_length=4096,  # 限制 token 使用
)

# 并行执行多个任务
import asyncio
results = await asyncio.gather(
    agent1.run(),
    agent2.run(),
    agent3.run(),
)

🔒 安全与合规

注意事项

  1. 遵守网站条款: 不要违反目标网站的服务条款
  2. 频率限制: 避免过于频繁的请求
  3. 数据隐私: 不要抓取敏感个人信息
  4. 验证码: 使用合法方式处理验证码
  5. 账号安全: 不要共享账号凭证

推荐做法

# 使用代理(避免 IP 被封)
browser = Browser(
    proxy="residential_proxy_url",
    user_agent="Mozilla/5.0...",
)

# 添加随机延迟
import random
import asyncio

await asyncio.sleep(random.uniform(2, 5))

# 使用云服务(内置反检测)
client = AsyncBrowserUse(
    stealth_mode=True,
    rotate_proxies=True,
)

📈 未来发展方向

技术趋势

  1. 多 Agent 协作: 多个 Agent 分工合作完成复杂任务
  2. 学习行为: 从演示中学习,减少 Prompt 依赖
  3. 增强隐私: 更好的隐私保护和安全控制
  4. 视觉理解: 更强的截图和视觉元素理解
  5. 工具集成: 与更多外部工具和 API 集成

应用场景扩展

  • 客户服务: 自动回复和处理客户问题
  • 市场研究: 自动收集和分析市场数据
  • 竞争监控: 监控竞争对手的价格和活动
  • 自动化测试: 端到端的 UI 测试
  • 个人助理: 帮助完成日常网络任务

🎓 学习资源

官方资源

  • GitHub: https://github.com/browser-use/browser-use
  • 文档: https://docs.browser-use.com
  • 云服务: https://cloud.browser-use.com
  • 基准测试: https://github.com/browser-use/benchmark
  • Web-UI: https://github.com/browser-use/web-ui

社区资源

  • Discord: Browser-Use 官方社区
  • Medium: 教程和案例分享
  • YouTube: 视频教程
  • Reddit: r/LocalLLaMA 讨论

推荐教程

  1. 入门: 官方 Quick Start
  2. 进阶: GitConnected 完整教程
  3. 实战: YouTube 视频教程
  4. 优化: 官方 Benchmark 分析

💡 总结与建议

何时使用 Browser-Use?

适合场景:

  • 网站没有 API 但需要自动化
  • 需要处理动态 JavaScript 内容
  • 多步骤的复杂 Web 任务
  • 需要 AI 决策的自动化
  • 快速原型开发

不适合场景:

  • 有官方 API 可用
  • 简单的数据抓取(用传统爬虫)
  • 高频、大批量任务(考虑专用工具)
  • 对延迟极其敏感的应用

快速开始建议

# 1. 安装
pip install browser-use

# 2. 获取 API Key(可选)
# 访问 cloud.browser-use.com

# 3. 运行第一个任务
python -c "
from browser_use import Agent
import asyncio
asyncio.run(Agent(task='打开 google.com 搜索 browser-use').run())
"

# 4. 查看示例
git clone https://github.com/browser-use/browser-use.git
cd browser-use/examples
Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐