lightpanda-io/browser
GitHub: lightpanda-io/browser
专为 AI 和自动化设计的高性能无头浏览器,以极低资源消耗实现 JavaScript 渲染和网页抓取。
Stars: 14589 | Forks: 502
Lightpanda 浏览器
lightpanda.io
[](https://github.com/lightpanda-io/browser/blob/main/LICENSE)
[](https://twitter.com/lightpanda_io)
[](https://github.com/lightpanda-io/browser)
Lightpanda 是一款专为 Headless 用途打造的开源浏览器:
- Javascript 执行
- 支持 Web APIs(部分支持,开发中)
- 通过 [CDP](https://chromedevtools.github.io/devtools-protocol/) 兼容 Playwright[^1]、Puppeteer、chromedp
为 AI 代理、LLM 训练、爬虫和测试提供极速的 Web 自动化:
- 超低内存占用(比 Chrome 少 9 倍)
- 极快的执行速度(比 Chrome 快 11 倍)
- 即时启动
[

](https://github.com/lightpanda-io/demo)
[

](https://github.com/lightpanda-io/demo)
_Puppeteer 在 AWS EC2 m5.large 实例上从本地网站请求 100 个页面。
查看[基准测试详情](https://github.com/lightpanda-io/demo)。_
[^1]: **Playwright 支持免责声明:**
由于 Playwright 的性质,在当前版本浏览器上运行的脚本可能无法在未来版本中正常运行。Playwright 使用一个中间 JavaScript 层,根据浏览器的可用功能选择执行策略。如果 Lightpanda 添加了新的 [Web API](https://developer.mozilla.org/en-US/docs/Web/API),Playwright 可能会为同一脚本选择执行不同的代码。这个新的代码路径可能会尝试使用尚未实现的功能。Lightpanda 致力于添加兼容性测试,但我们无法覆盖所有场景。如果您遇到问题,请创建一个 [GitHub issue](https://github.com/lightpanda-io/browser/issues) 并包含该脚本最后已知可用的版本。
## 快速开始
### 安装
**从每日构建版本安装**
您可以从 [ nightly builds](https://github.com/lightpanda-io/browser/releases/tag/nightly) 下载适用于 Linux x86_64 和 MacOS aarch64 的最新二进制文件。
*针对 Linux*
```
curl -L -o lightpanda https://github.com/lightpanda-io/browser/releases/download/nightly/lightpanda-x86_64-linux && \
chmod a+x ./lightpanda
```
*针对 MacOS*
```
curl -L -o lightpanda https://github.com/lightpanda-io/browser/releases/download/nightly/lightpanda-aarch64-macos && \
chmod a+x ./lightpanda
```
*针对 Windows + WSL2*
Lightpanda 浏览器兼容在 WSL 内部的 Windows 上运行。请按照 Linux 说明从 WSL 终端进行安装。
建议在 Windows 主机上安装 Puppeteer 等客户端。
**通过 Docker 安装**
Lightpanda 为 Linux amd64 和 arm64 架构提供了[官方 Docker 镜像](https://hub.docker.com/r/lightpanda/browser)。
以下命令获取 Docker 镜像并启动一个新容器,在端口 `9222` 上暴露 Lightpanda 的 CDP 服务器。
```
docker run -d --name lightpanda -p 9222:9222 lightpanda/browser:nightly
```
### 转储 URL
```
./lightpanda fetch --obey_robots --log_format pretty --log_level info https://demo-browser.lightpanda.io/campfire-commerce/
```
```
INFO telemetry : telemetry status . . . . . . . . . . . . . [+0ms]
disabled = false
INFO page : navigate . . . . . . . . . . . . . . . . . . . . [+6ms]
url = https://demo-browser.lightpanda.io/campfire-commerce/
method = GET
reason = address_bar
body = false
req_id = 1
INFO browser : executing script . . . . . . . . . . . . . . [+118ms]
src = https://demo-browser.lightpanda.io/campfire-commerce/script.js
kind = javascript
cacheable = true
INFO http : request complete . . . . . . . . . . . . . . . . [+140ms]
source = xhr
url = https://demo-browser.lightpanda.io/campfire-commerce/json/product.json
status = 200
len = 4770
INFO http : request complete . . . . . . . . . . . . . . . . [+141ms]
source = fetch
url = https://demo-browser.lightpanda.io/campfire-commerce/json/reviews.json
status = 200
len = 1615
```
### 启动 CDP 服务器
```
./lightpanda serve --obey_robots --log_format pretty --log_level info --host 127.0.0.1 --port 9222
```
```
INFO telemetry : telemetry status . . . . . . . . . . . . . [+0ms]
disabled = false
INFO app : server running . . . . . . . . . . . . . . . . . [+0ms]
address = 127.0.0.1:9222
```
CDP 服务器启动后,您可以通过配置 `browserWSEndpoint` 来运行 Puppeteer 脚本。
```
'use strict'
import puppeteer from 'puppeteer-core';
// use browserWSEndpoint to pass the Lightpanda's CDP server address.
const browser = await puppeteer.connect({
browserWSEndpoint: "ws://127.0.0.1:9222",
});
// The rest of your script remains the same.
const context = await browser.createBrowserContext();
const page = await context.newPage();
// Dump all the links from the page.
await page.goto('https://demo-browser.lightpanda.io/amiibo/', {waitUntil: "networkidle0"});
const links = await page.evaluate(() => {
return Array.from(document.querySelectorAll('a')).map(row => {
return row.getAttribute('href');
});
});
console.log(links);
await page.close();
await context.close();
await browser.disconnect();
```
### 遥测
默认情况下,Lightpanda 会收集并发送使用遥测数据。可以通过设置环境变量 `LIGHTPANDA_DISABLE_TELEMETRY=true` 来禁用此功能。您可以在以下网址阅读 Lightpanda 的隐私政策:[https://lightpanda.io/privacy-policy](https://lightpanda.io/privacy-policy)。
## 状态
Lightpanda 处于 Beta 阶段,目前正在开发中。稳定性和覆盖率正在提高,许多网站现在可以正常工作。
您可能仍会遇到错误或崩溃。如果是这样,请具体描述并开启一个 issue。
以下是我们已实现的关键功能:
- [x] HTTP 加载器 ([Libcurl](https://curl.se/libcurl/))
- [x] HTML 解析器 ([html5ever](https://github.com/servo/html5ever))
- [x] DOM 树
- [x] Javascript 支持 ([v8](https://v8.dev/))
- [x] DOM APIs
- [x] Ajax
- [x] XHR API
- [x] Fetch API
- [x] DOM 转储
- [x] CDP/websockets 服务器
- [x] 点击
- [x] 输入表单
- [x] Cookies
- [x] 自定义 HTTP 头
- [x] 代理支持
- [x] 网络拦截
- [x] 使用 `--obey_robots` 选项遵循 `robots.txt`
注意:Web APIs 数以百计。开发一款浏览器(即使仅用于 Headless 模式)是一项艰巨的任务。覆盖率将随着时间的推移而增加。
您也可以在我们专门的 [zig-js-runtime](https://github.com/lightpanda-io/zig-js-runtime#development) 项目中关注我们 Javascript 支持的进展。
## 从源代码构建
### 前置条件
Lightpanda 使用 [Zig](https://ziglang.org/) `0.15.2` 编写。您必须安装正确版本才能构建项目。
Lightpanda 还依赖于 [zig-js-runtime](https://github.com/lightpanda-io/zig-js-runtime/)(带有 v8)、[Libcurl](https://curl.se/libcurl/) 和 [html5ever](https://github.com/servo/html5ever)。
为了能够为 zig-js-runtime 构建 v8 引擎,您需要安装一些库:
对于 **Debian/Ubuntu 系 Linux**:
```
sudo apt install xz-utils ca-certificates \
pkg-config libglib2.0-dev \
clang make curl git
```
您还需要[安装 Rust](https://rust-lang.org/tools/install/)。
对于带有 [**Nix**](https://nixos.org/download/) 的系统,您可以使用 devShell:
```
nix develop
```
对于 **MacOS**,您需要 cmake 和 [Rust](https://rust-lang.org/tools/install/)。
```
brew install cmake
```
### 构建并运行
您可以使用 `make build` 构建整个浏览器,或者在调试环境下使用 `make build-dev`。
但您也可以直接使用 zig 命令:`zig build run`。
#### 嵌入 v8 快照
Lighpanda 使用 v8 快照。默认情况下,它是在启动时创建的,但您可以通过使用以下命令将其嵌入:
生成快照。
```
zig build snapshot_creator -- src/snapshot.bin
```
使用快照二进制文件进行构建。
```
zig build -Dsnapshot_path=../../snapshot.bin
```
有关更多详细信息,请参见 [#1279](https://github.com/lightpanda-io/browser/pull/1279)。
## 测试
### 单元测试
您可以通过运行 `make test` 来测试 Lightpanda。
### 端到端测试
要运行端到端测试,您需要将 [演示仓库](https://github.com/lightpanda-io/demo) 克隆到 `../demo` 目录中。
您必须安装 [演示的 node 需求](https://github.com/lightpanda-io/demo?tab=readme-ov-file#dependencies-1)
您还需要安装 [Go](https://go.dev) > v1.24。
```
make end2end
```
### Web 平台测试
Lightpanda 是针对标准化的 [Web 平台测试](https://web-platform-tests.org/) 进行测试的。
我们使用包含自定义 [`testharnessreport.js`](https://github.com/lightpanda-io/wpt/commit/01a3115c076a3ad0c84849dbbf77a6e3d199c56f) 的 [一个分叉](https://github.com/lightpanda-io/wpt/tree/fork)。
作为参考,您可以通过 [wpt.live](https://wpt.live) 轻松地使用您的浏览器执行 WPT 测试用例。
#### 配置 WPT HTTP 服务器
要运行测试,您必须克隆仓库,配置自定义 hosts 并生成 `MANIFEST.json` 文件。
使用 `fork` 分支克隆仓库。
```
git clone -b fork --depth=1 git@github.com:lightpanda-io/wpt.git
```
进入 `wpt/` 目录。
在您的 `/etc/hosts` 中安装自定义域
```
./wpt make-hosts-file | sudo tee -a /etc/hosts
```
生成 `MANIFEST.json`
```
./wpt manifest
```
有关详细信息,请使用 [WPT 的设置指南](https://web-platform-tests.org/running-tests/from-local-system.html)。
#### 运行 WPT 测试套件
[github.com/lightpanda-io/demo/](https://github.com/lightpanda-io/demo/) 仓库提供了一个外部的 [Go](https://go.dev) 运行器,位于 `wptrunner/` 目录中。
您需要先克隆该项目。
首先从您的 `wpt/` 克隆目录启动 WPT 的 HTTP 服务器。
```
./wpt serve
```
运行 Lightpanda 浏览器
```
zig build run -- --insecure_disable_tls_host_verification
```
然后您可以从 Demo 的克隆目录启动 wptrunner:
```
cd wptrunner && go run .
```
或者运行一个特定的测试:
```
cd wptrunner && go run . Node-childNodes.html
```
`wptrunner` 命令接受修改输出的 `--summary` 和 `--json` 选项。
还有 `--concurrency` 定义并发限制。
:warning: 运行整个测试套件将花费很长时间。在这种情况下,以 `releaseFast` 模式构建以加快测试速度是很有用的。
```
zig build -Doptimize=ReleaseFast run
```
## 贡献
Lightpanda 通过 GitHub 接受 Pull Requests。
您必须在 Pull Request 过程中签署我们的 [CLA](CLA.md),否则我们将无法接受您的贡献。
## 为什么?
### Javascript 执行对于现代 Web 至关重要
在过去,抓取网页就像发起一个 HTTP 请求一样简单,就像 cURL。但这已不再可能,因为 Javascript 无处不在,无论您是否喜欢:
- Ajax、单页应用、无限加载、“点击显示”、即时搜索等。
- JS web 框架:React、Vue、Angular 及其他
### Chrome 不是正确的工具
如果我们需要 Javascript,为什么不使用真正的 Web 浏览器呢?拿一个庞大的桌面应用程序,对其进行修改,然后在服务器上运行它。如果您大规模使用它,可能需要数百或数千个 Chrome 实例。您确定这是个好主意吗?
- 占用大量 RAM 和 CPU,运行成本高昂
- 难以大规模打包、部署和维护
- 臃肿,许多功能在 Headless 用法中毫无用处
### Lightpanda 为性能而生
如果我们想要在一个真正的 Headless 浏览器中同时拥有 Javascript 和性能,我们需要从头开始。不是 Chromium 的另一次迭代,而是真正从空白页开始。很疯狂对吧?但这正是我们所做的:
- 不基于 Chromium、Blink 或 WebKit
- 底层系统编程语言(Zig),以优化为理念
- 独断专行:没有图形渲染标签:AI代理, CDP协议, JavaScript引擎, Playwright, Puppeteer, RPA, Web API, Web爬虫, Zig语言, 人工智能, 低内存占用, 命令控制, 大语言模型训练, 开源浏览器, 数据泄露, 数据采集, 无头浏览器, 浏览器内核, 用户模式Hook绕过, 网络信息收集, 网页自动化, 请求拦截, 高性能计算