Obscura — Rust 编写的轻量级无头浏览器

什么是 Obscura

Obscura 是一个用 Rust 编写的开源无头浏览器引擎,专为 web scraping 和 AI agent 自动化场景设计。它通过 V8 引擎执行真实的 JavaScript,支持 Chrome DevTools Protocol (CDP),可作为 Puppeteer 和 Playwright 的替代方案直接使用。

核心优势对比

指标 Obscura Headless Chrome
内存占用 30 MB 200+ MB
二进制大小 70 MB 300+ MB
抗检测 内置
页面加载 85 ms ~500 ms
启动时间 即时 ~2s

安装

下载二进制

1
2
3
4
5
6
7
# Linux x86_64
curl -LO https://github.com/h4ckf0r0day/obscura/releases/latest/download/obscura-x86_64-linux.tar.gz
tar xzf obscura-x86_64-linux.tar.gz

# macOS Apple Silicon
curl -LO https://github.com/h4ckf0r0day/obscura/releases/latest/download/obscura-aarch64-macos.tar.gz
tar xzf obscura-aarch64-macos.tar.gz

无需安装 Chrome、Node.js 或任何依赖。tar.gz 包内含 obscuraobscura-worker 两个文件,放在同一目录下即可使用 scrape 并行命令。

源码编译

1
2
3
4
5
6
git clone https://github.com/h4ckf0r0day/obscura.git
cd obscura
cargo build --release

# 开启 stealth 模式(抗检测 + 追踪器拦截)
cargo build --release --features stealth

需要 Rust 1.75+。首次编译约 5 分钟(V8 从源码编译,之后会缓存)。

基本用法

obscura fetch — 获取单个页面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 获取页面标题
obscura fetch https://example.com --eval "document.title"

# 提取所有链接
obscura fetch https://example.com --dump links

# 渲染 JS 并输出 HTML
obscura fetch https://news.ycombinator.com --dump html

# 等待动态内容加载
obscura fetch https://example.com --wait-until networkidle0

# 设置超时
obscura fetch https://example.com --timeout 10

obscura serve — 启动 CDP Server

1
2
3
4
obscura serve --port 9222

# 开启 stealth 模式
obscura serve --port 9222 --stealth

启动后可通过 WebSocket 连接 ws://127.0.0.1:9222/devtools/browser,然后用 Puppeteer/Playwright 控制浏览器。

obscura scrape — 并行抓取

1
2
3
4
obscura scrape url1 url2 url3 \
--concurrency 25 \
--eval "document.querySelector('h1').textContent" \
--format json

配合 Puppeteer 使用

1
npm install puppeteer-core
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import puppeteer from 'puppeteer-core';

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

const page = await browser.newPage();
await page.goto('https://news.ycombinator.com');

const stories = await page.evaluate(() =>
Array.from(document.querySelectorAll('.titleline > a'))
.map(a => ({ title: a.textContent, url: a.href }))
);
console.log(stories);

await browser.disconnect();

配合 Playwright 使用

1
npm install playwright-core
1
2
3
4
5
6
7
8
9
10
11
import { chromium } from 'playwright-core';

const browser = await chromium.connectOverCDP({
endpointURL: 'ws://127.0.0.1:9222',
});

const page = await browser.newContext().then(ctx => ctx.newPage());
await page.goto('https://en.wikipedia.org/wiki/Web_scraping');
console.log(await page.title());

await browser.close();

表单提交与登录

1
2
3
4
5
6
7
await page.goto('https://quotes.toscrape.com/login');
await page.evaluate(() => {
document.querySelector('#username').value = 'admin';
document.querySelector('#password').value = 'admin';
document.querySelector('form').submit();
});
// Obscura 会处理 POST、跟随 302 重定向、维持 cookies

Stealth 模式

编译时加上 --features stealth,运行时加 --stealth 即可启用。

抗指纹

  • 每次会话随机化 GPU、屏幕、Canvas、Audio、电池指纹
  • 真实的 navigator.userAgentData (Chrome 145,高熵值)
  • event.isTrusted = true(浏览器原生事件标记)
  • 隐藏内部属性(Object.keys(window) 不暴露内部信息)
  • Function.prototype.toString() 伪装为 [native code]
  • navigator.webdriver = undefined(与真实 Chrome 一致)

追踪器拦截

  • 覆盖 3,520 个域名
  • 自动拦截 analytics、ads、telemetry、fingerprinting 脚本

CDP API 支持范围

Domain 支持的方法
Target createTarget, closeTarget, attachToTarget, createBrowserContext, disposeBrowserContext
Page navigate, getFrameTree, addScriptToEvaluateOnNewDocument, lifecycleEvents
Runtime evaluate, callFunctionOn, getProperties, addBinding
DOM getDocument, querySelector, querySelectorAll, getOuterHTML, resolveNode
Network enable, setCookies, getCookies, setExtraHTTPHeaders, setUserAgentOverride
Fetch enable, continueRequest, fulfillRequest, failRequest(实时拦截)
Storage getCookies, setCookies, deleteCookies
Input dispatchMouseEvent, dispatchKeyEvent
LP getMarkdown(DOM 转 Markdown)

CLI 速查

obscura serve

参数 默认值 说明
--port 9222 WebSocket 端口
--proxy HTTP/SOCKS5 代理 URL
--stealth off 抗检测 + 追踪拦截
--workers 1 并行 worker 进程数
--obey-robots off 遵守 robots.txt

obscura fetch <URL>

参数 默认值 说明
--dump html 输出格式:html / text / links
--eval 要执行的 JavaScript 表达式
--wait-until load 等待时机:load / domcontentloaded / networkidle0
--timeout 30 最大导航时间(秒)
--selector 等待 CSS 选择器出现
--stealth off 抗检测模式
--quiet off 隐藏 banner 输出

obscura scrape <URL...>

参数 默认值 说明
--concurrency 10 并行 worker 数
--eval 每页执行的 JS 表达式
--format json 输出格式:json / text

性能基准

页面类型 Obscura Chrome
静态 HTML 51 ms ~500 ms
JS + XHR + fetch 84 ms ~800 ms
动态脚本 78 ms ~700 ms

适用场景

Obscura 适合以下场景:

  • AI Agent 自动化:需要真实浏览器环境执行 JavaScript 的 AI 代理
  • 大规模 Web Scraping:低内存 + 并行抓取,资源消耗远低于 Puppeteer/Playwright + Chrome
  • 需要抗检测的场景:内置 stealth 模式,无需额外配置
  • CDP 工具链:已有 Puppeteer/Playwright 代码可零成本迁移

不适用于需要完整浏览器 UI 交互的场景(如复杂表单、文件上传、浏览器扩展等)。


项目地址: github.com/h4ckf0r0day/obscura,Apache 2.0 开源协议


Obscura — Rust 编写的轻量级无头浏览器
https://kingjem.github.io/2026/05/07/obscura-基于-webrtc-流量封装的可插拔传输/
作者
Ruhai
发布于
2026年5月7日
许可协议