🚀 快速安装

复制以下命令并运行,立即安装此 Skill:

npx skills add https://skills.sh/aradotso/trending-skills/lightpanda-browser

💡 提示:需要 Node.js 和 NPM

Lightpanda — 面向 AI 与自动化的无头浏览器

技能来自 ara.so — 每日 2026 技能合集

Lightpanda 是一个用 Zig 从头构建的无头浏览器,专为 AI 代理、网页抓取和自动化而设计。它的内存使用量比 Chrome 无头模式少 9 倍,运行速度快 11 倍。

关键特性:

  • 不基于 Chromium、Blink 或 WebKit — 完全由 Zig 全新实现
  • 通过 V8 引擎执行 JavaScript
  • 兼容 CDP(Chrome DevTools Protocol)— 可与 Playwright、Puppeteer、chromedp 配合使用
  • 通过 --obey_robots 标志遵守 robots.txt
  • 测试版,积极开发中
  • 许可证:AGPL-3.0

安装

macOS (Apple Silicon)

curl -L -o lightpanda https://github.com/lightpanda-io/browser/releases/download/nightly/lightpanda-aarch64-macos
chmod a+x ./lightpanda

Linux (x86_64)

curl -L -o lightpanda https://github.com/lightpanda-io/browser/releases/download/nightly/lightpanda-x86_64-linux
chmod a+x ./lightpanda

Docker

# 支持 amd64 和 arm64 架构
docker run -d --name lightpanda -p 9222:9222 lightpanda/browser:nightly

CLI 用法

获取 URL(输出渲染后的 HTML)

./lightpanda fetch --obey_robots --log_format pretty --log_level info https://example.com

启动 CDP 服务器

./lightpanda serve --obey_robots --log_format pretty --log_level info --host 127.0.0.1 --port 9222

这将启动一个基于 WebSocket 的 CDP 服务器,用于程序化控制。

CLI 标志

标志 描述
--obey_robots 遵守 robots.txt 规则
--log_format pretty 人类可读的日志输出
--log_level info 日志详细程度:debuginfowarnerror
--host 127.0.0.1 CDP 服务器绑定地址
--port 9222 CDP 服务器端口
--insecure_disable_tls_host_verification 禁用 TLS 验证(仅限测试)

Playwright 集成

启动 CDP 服务器,然后将 Playwright 连接到它:

import { chromium } from 'playwright-core';

const browser = await chromium.connectOverCDP('http://127.0.0.1:9222');
const context = await browser.contexts()[0] || await browser.newContext();
const page = await context.newPage();

await page.goto('https://example.com', { waitUntil: 'networkidle' });
const title = await page.title();
const content = await page.content();

console.log(`标题: ${title}`);
console.log(`HTML 长度: ${content.length}`);

await browser.close();

Puppeteer 集成

import puppeteer from 'puppeteer-core';

const browser = await puppeteer.connect({
  browserWSEndpoint: 'ws://127.0.0.1:9222',
});

const context = await browser.createBrowserContext();
const page = await context.newPage();

await page.goto('https://example.com', { waitUntil: 'networkidle0' });

const title = await page.title();
const text = await page.evaluate(() => document.body.innerText);

console.log(`标题: ${title}`);
console.log(`正文文本: ${text.substring(0, 200)}`);

await page.close();
await browser.close();

Go (chromedp) 集成

package main

import (
    "context"
    "fmt"
    "log"

    "github.com/chromedp/chromedp"
)

func main() {
    allocCtx, cancel := chromedp.NewRemoteAllocator(context.Background(), "ws://127.0.0.1:9222")
    defer cancel()

    ctx, cancel := chromedp.NewContext(allocCtx)
    defer cancel()

    var title string
    err := chromedp.Run(ctx,
        chromedp.Navigate("https://example.com"),
        chromedp.Title(&title),
    )
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println("标题:", title)
}

Python 集成

import asyncio
from playwright.async_api import async_playwright

async def main():
    async with async_playwright() as p:
        browser = await p.chromium.connect_over_cdp("http://127.0.0.1:9222")
        context = browser.contexts[0] if browser.contexts else await browser.new_context()
        page = await context.new_page()

        await page.goto("https://example.com", wait_until="networkidle")
        title = await page.title()
        content = await page.content()

        print(f"标题: {title}")
        print(f"HTML 长度: {len(content)}")

        await browser.close()

asyncio.run(main())

网页抓取模式

批量页面获取

import { chromium } from 'playwright-core';

const browser = await chromium.connectOverCDP('http://127.0.0.1:9222');
const context = await browser.newContext();

const urls = [
  'https://example.com/page1',
  'https://example.com/page2',
  'https://example.com/page3',
];

for (const url of urls) {
  const page = await context.newPage();
  await page.goto(url, { waitUntil: 'networkidle' });

  const data = await page.evaluate(() => ({
    title: document.title,
    text: document.body.innerText,
    links: [...document.querySelectorAll('a[href]')].map(a => a.href),
  }));

  console.log(JSON.stringify(data, null, 2));
  await page.close();
}

await browser.close();

提取结构化数据

const data = await page.evaluate(() => {
  const items = document.querySelectorAll('.product-card');
  return [...items].map(item => ({
    name: item.querySelector('h2')?.textContent?.trim(),
    price: item.querySelector('.price')?.textContent?.trim(),
    link: item.querySelector('a')?.href,
  }));
});

Docker Compose(与你的应用配合)

services:
  lightpanda:
    image: lightpanda/browser:nightly
    ports:
      - "9222:9222"
    restart: unless-stopped

  scraper:
    build: .
    depends_on:
      - lightpanda
    environment:
      - BROWSER_WS_ENDPOINT=ws://lightpanda:9222

支持的 Web API

Lightpanda 支持(部分实现,持续扩展中):

  • DOM 树操作和查询
  • JavaScript 执行(V8)
  • XMLHttpRequest (XHR)
  • Fetch API
  • Cookie 管理
  • 网络拦截
  • 代理支持

配置

环境变量 描述
LIGHTPANDA_DISABLE_TELEMETRY 设置为 true 以选择退出使用统计

性能对比

指标 Lightpanda Chrome 无头模式
内存 约少 9 倍 基准
速度 约快 11 倍 基准
二进制大小 小(Zig) 大(Chromium)
渲染 无视觉渲染 完整渲染引擎

何时使用 Lightpanda

使用 Lightpanda 的场景:

  • 运行需要浏览网页的 AI 代理
  • 大规模批量抓取(内存/CPU 节约很重要)
  • 自动化表单提交和数据提取
  • 在资源受限的容器中运行
  • 需要 CDP 兼容性但不需要完整的视觉渲染

使用 Chrome/Playwright 的场景:

  • 需要像素完美的截图或 PDF 生成
  • 需要完整的 Web API 覆盖(Lightpanda 仍不完整)
  • 视觉回归测试
  • 测试浏览器特定的渲染行为

从源码构建

需要:Zig 0.15.2、Rust、CMake、系统依赖。

# Ubuntu/Debian 依赖
sudo apt install xz-utils ca-certificates pkg-config libglib2.0-dev clang make curl

# 构建
git clone https://github.com/lightpanda-io/browser.git
cd browser
zig build

# 可选:预构建 V8 快照以加快启动速度
zig build snapshot_creator -- src/snapshot.bin
zig build -Dsnapshot_path=../../snapshot.bin

故障排除

端口 9222 连接被拒绝:

  • 确保 ./lightpanda serve 正在运行
  • 如果从 Docker/远程连接,检查 --host 0.0.0.0

Playwright 脚本更新后中断:

  • Lightpanda 是测试版 — Playwright 的能力检测可能在不同版本间表现不同
  • 固定你的 Lightpanda 版本或一致地使用 nightly 版本

缺少 Web API 支持:

链接

📄 原始文档

完整文档(英文):

https://skills.sh/aradotso/trending-skills/lightpanda-browser

💡 提示:点击上方链接查看 skills.sh 原始英文文档,方便对照翻译。

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。