如何使用 Firefox 通过命令行将 html 导出到文件

问题描述 投票:0回答:4

所以这个问题本身就很好解释了。

是否有控制台命令行可以用firefox保存html?

谢谢。

firefox command-line
4个回答
18
投票

无法通过命令行要求 Firefox 保存页面(截至 2015 年 5 月)。然而,有一个脚本可以自动执行启动 Firefox、保存页面、退出 Firefox 的过程。

https://github.com/abiyani/automate-save-page-as

来自自述文件:

当 wget 无法解决问题时的快速破解。

tl;dr 从命令行执行浏览器的“页面另存为”(Ctrl+S) 操作,无需手动干预

这个小型 bash 脚本模拟一系列按键,在浏览器中打开给定的 url,保存页面 (Ctrl+S),然后关闭浏览器选项卡/窗口 (Ctrl+F4)。

# Use Firefox to open a web-page and save it in /tmp
# (the default name for the file (Page title) is used)
$ ./save_page_as "www.example.com" --browser "firefox" --destination "/tmp" 

12
投票

使用 wget 的建议无法处理访问内容需要特殊登录或其他身份验证的情况,这些身份验证需要在浏览器内部执行某些用户操作(例如,将身份验证结果存储在 firefox 的 cert8.db 或 keys.db 中或Signons.sqlite 甚至在 locatsore.rdf 中)。如果身份验证是通过加载的网页内的 javascript 完成的,则使用 Firefox cookie 的 wget --load-cookies 可能无法工作。

所以用户想要某种方式来运行 Firefox,如下所示:

firefox "<some-url-with-complex-authentication>" -save-to-folder ./somewhere

(Firefox 在完成保存获取的 URL 后退出)。 是的,它很重,但是如果您可以在浏览器中查看页面但不能通过 wget 或类似的方式查看页面,那么目前似乎没有任何方法可以从命令行使用它。

但是,可能只是用户没有从 FF 导出最新的 cookies 文件,以便可以通过 wget --load-cookies 加载它,因为创建 wget 兼容(netscape 风格)的 cookies 文件需要: 1)安装Export Cookies之类的插件 2)导出cookies.txt文件供wget使用。


2
投票

我在 Windows 上使用 PowerShell 时使用这种方式:

[system.Diagnostics.Process]::Start("Firefox","https://stackoverflow.com/questions/15429745/how-can-i-export-html-to-file-via-command-line-with-firefox")
Sleep 2;

$obj_Shell = New-Object -ComObject wscript.shell;
$obj_Shell.AppActivate('Firefox');
Sleep 1;

$obj_Shell.SendKeys("^(s)");
Sleep 3;

$obj_Shell.SendKeys("{ENTER}");

Select-String -Path $env:USERPROFILE + "\Downloads\how-can-i-export-html-to-file-via-command-line-with-firefox.html"

0
投票

使用

pup
。与 automate-save-page-as 不同,
pup
不保存原始 HTML,而是输出解析后的 HTML。

© www.soinside.com 2019 - 2024. All rights reserved.