SingleFile CLI:网页单文件保存工具
在命令行中把完整网页保存为单个 HTML 文件的工具,基于 SingleFile,适合归档与抓取。
- 浏览
- 1
- 访问
- 0
- 下载
- 0
- Stars
- 1,717
- 版本
- v2.31.0
- 平台
- Windows、macOS、Linux
详细介绍
项目简介
SingleFile CLI 是 SingleFile 的命令行版本。它通过(无头)浏览器启动,把网页的 HTML、CSS、图片等资源内联保存为一个忠实的单文件 HTML 副本。
它通过 Chrome DevTools Protocol 将脚本注入网页运行(使用 Firefox 时走 WebDriver BiDi),而不是以浏览器扩展的形式嵌入。
主要功能
- 将完整网页保存为单个 HTML 文件,或在控制台输出页面内容
- 支持 Chrome / Chromium 系浏览器,也可通过
--browser-engine firefox使用 Firefox - 支持批量保存:从文件中读取 URL 列表
- 支持爬取链接:可限制只抓内部链接、限制深度、按规则重写 URL
- 可将爬取结果保存为自解压 ZIP 文件,并标记未归档的链接
- 支持登录后页面:先创建并保存浏览器 profile,之后重复复用
--manifest记录保存过程,便于事后验证--add-proof生成 OpenTimestamps 存在性证明- 支持自定义样式表与注入脚本,处理标签页切换等特殊页面
- 提供 Docker 镜像,也可编译为独立可执行文件
安装与使用
最简单的用法是直接下载可执行文件,放到任意目录:
https://github.com/gildas-lormeau/single-file-cli/releases
请确保默认目录中安装了 Chrome 或 Chromium 系浏览器;否则需要通过 --browser-executable-path 指定浏览器可执行文件路径。
通过 npm 安装(需要先安装 Deno):
npm install "single-file-cli"
npx single-file ...
通过 Docker 使用:
docker pull capsulecode/singlefile
docker tag capsulecode/singlefile singlefile
docker run singlefile "https://www.wikipedia.org"
基本语法:
single-file <url> [output] [options ...]
常用示例:
single-file https://www.wikipedia.org wikipedia.html
single-file https://www.wikipedia.org --dump-content
single-file --urls-file=list-urls.txt
single-file https://www.wikipedia.org wikipedia.html --browser-engine firefox
爬取站内链接并去掉查询参数:
single-file https://www.wikipedia.org --crawl-links=true --crawl-inner-links-only=true --crawl-max-depth=1 --crawl-rewrite-rule="^(.*)\\?.*$ $1"
保存需要登录的页面:
single-file --create-browser-profile ./profiles/example https://www.example.com/login
single-file https://www.example.com/account account.html --browser-profile=./profiles/example
为保存的页面添加存在性证明:
single-file https://www.wikipedia.org wikipedia.html --add-proof
npx opentimestamps upgrade wikipedia.html.ots
npx opentimestamps verify wikipedia.html.ots
退出码:全部页面保存成功为 0,至少一个页面失败为 1(其余页面仍会保存),进程无法运行为 255。
更多选项可通过 single-file --help 查看。
注意事项
- 在 macOS 和 Windows 上,Chromium 系浏览器默认启用沙箱;若浏览器启动即退出,工具会以
--no-sandbox重新启动并给出警告。 - 在 Linux 上浏览器以
--no-sandbox启动。 - 使用 Flatpak 版 Firefox 时需允许其访问
/tmp:
flatpak override --user --filesystem=/tmp org.mozilla.firefox
- 存在性证明只对保存时那个确切的文件有效,请将
.ots文件与页面放在一起,且不要修改页面;证明丢失后无法为同一时间重新生成。 - 项目采用 AGPL-3.0 许可证,第三方项目衍生的代码使用 MIT 许可证。
本作品整理自 GitHub 开源项目 gildas-lormeau/single-file-cli,原作者 @gildas-lormeau,许可证 AGPL-3.0。中文介绍由 AI 根据项目 README 整理,以原仓库为准。
更新记录
- v2.31.0当前版本
新增 `{singlefile-name}`、`{singlefile-version}`、`{singlefile-url}` 三个模板变量,可用于 `--filename-template` 和 `--infobar-template`;并更新 single-file-core 至 1.22.0,修复无法保存的图片、损坏图片及 srcset 候选图的占位与处理问题。
评论