8

Chrome 的文档指出:

--dump-dom 标志将 document.body.innerHTML 打印到标准输出:

根据标题,如何使用 Chromium 无头转储更多的 DOM 对象(理想情况下是全部)?我可以通过开发者工具手动保存整个 DOM,但我想要一个程序化的解决方案。

4

2 回答 2

6

更新 2019-04-23 谷歌在无头前端非常活跃,发生了许多更新

下面的答案适用于 v62 当前版本是 v73 并且它一直在更新。 https://www.chromestatus.com/features/schedule

我强烈建议使用无头 chrome 检查 puppeteer 的任何未来开发。它由 Google 维护,它与npm 包一起安装所需的 Chrome 版本,因此您只需使用文档中的 puppeteer API,而不必担心 Chrome 版本并设置无头 Chrome 和开发工具 API 之间的连接,这可以完成 99% 的魔法.


2017 年 10 月 29 日更新 Chrome 已经有了 --dump-html 标志,它返回完整的 HTML,而不仅仅是正文。

v62 确实有,它已经在稳定的频道上。

解决此问题的问题:https ://bugs.chromium.org/p/chromium/issues/detail?id=752747

当前 chrome 状态(每个频道的版本)https://www.chromestatus.com/features/schedule

为遗产留下旧答案

您可以使用 google chrome 远程界面进行操作。我已经尝试过了,浪费了几个小时试图启动 chrome 并获得完整的 html,包括标题,但它还没有准备好,我想说。

它有时可以工作,但我尝试在生产环境中运行它并且不时出错。各种随机错误,例如 connection resetno chrome found to kill。这些错误有时会出现,并且很难调试。

--dump-dom当我需要正文和需要标题时,我个人使用来获取 html,我curl现在只使用它。当然,chrome 可以从 SPA 应用程序中为您提供标题,如果标题是从 JS 设置的,则仅使用 curl 是无法完成的。有稳定的解决方案后将切换到谷歌浏览器。

很想--dump-html在 chrome 上有标志,然后得到所有的 html。如果 Google 的工程师正在阅读此内容,请将此类标志添加到 chrome。

我在 Chrome 问题跟踪器上创建了问题,请单击最喜欢的“星”以引起谷歌开发人员的注意:

https://bugs.chromium.org/p/chromium/issues/detail?id=752747

这是 chrome 的各种标志的长列表,不确定它是否已满和所有标志: https ://peter.sh/experiments/chromium-command-line-switches/没有转储标题标签。

这段代码来自谷歌的博客文章,你可以试试这个:

const CDP = require('chrome-remote-interface');

...

(async function() {

const chrome = await launchChrome();
const protocol = await CDP({port: chrome.port});

// Extract the DevTools protocol domains we need and enable them.
// See API docs: https://chromedevtools.github.io/devtools-protocol/
const {Page, Runtime} = protocol;
await Promise.all([Page.enable(), Runtime.enable()]);

Page.navigate({url: 'https://www.chromestatus.com/'});

// Wait for window.onload before doing stuff.
Page.loadEventFired(async () => {
  const js = "document.querySelector('title').textContent";
  // Evaluate the JS expression in the page.
  const result = await Runtime.evaluate({expression: js});

  console.log('Title of page: ' + result.result.value);

  protocol.close();
  chrome.kill(); // Kill Chrome.
});

})();

来源: https ://developers.google.com/web/updates/2017/04/headless-chrome

于 2017-08-05T11:30:43.903 回答
0

您缺少--headless获得标准输出的机会。

chromium --incognito \
         --proxy-auto-detect \
         --temp-profile \ 
         --headless \
         --dump-dom https://127.0.0.1:8080/index.html

将其全部输入 | html2text以将 html 重新编译为文本。

于 2021-03-08T19:00:44.510 回答