Wget输出文档和标题到STDOUT

use*_*398 92 linux http wget

我正在尝试使用wget by将文档正文及其标题输出到stdout wget -S -O - http://google.com

但它只显示html docment.

谢谢

UPD:

工作了这个 wget --save-headers --output-document - http://google.com

wget --version 显示GNU Wget 1.11.4 Red Hat已修改

Jos*_*ust 150

尝试以下操作,无额外标题

wget -qO- www.google.com
Run Code Online (Sandbox Code Playgroud)

请注意尾随-.这是-O传递给文件的正常命令参数的一部分,但由于我们不使用>直接指向文件,因此它会发送到shell.你可以使用-qO--qO -.

  • 这个答案没有任何意义。OP要求显示标题,而不是隐藏它们 (22认同)
  • 我的`alpine` linux容器不支持`-S`选项.我省略了它,一切都很好 (3认同)
  • O之后是什么? (2认同)

thk*_*ala 43

wget -S -O - http://google.com按预期工作,有一点需要注意:标题被认为是调试信息,因此它们被发送到标准错误而不是标准输出.如果要将标准输出重定向到文件或其他进程,则只能获取文档内容.

您可以尝试将标准错误重定向到标准输出作为可能的解决方案.例如,在bash:

$ wget -q -S -O - 2>&1 | grep ...
Run Code Online (Sandbox Code Playgroud)

要么

$ wget -q -S -O - 1>wget.txt 2>&1
Run Code Online (Sandbox Code Playgroud)

-q选项会抑制进度条和wget输出中一些令人讨厌的繁琐部分.


Ben*_*ela 19

它在这里工作:

    $ wget -S -O - http://google.com
HTTP request sent, awaiting response... 
  HTTP/1.1 301 Moved Permanently
  Location: http://www.google.com/
  Content-Type: text/html; charset=UTF-8
  Date: Sat, 25 Aug 2012 10:15:38 GMT
  Expires: Mon, 24 Sep 2012 10:15:38 GMT
  Cache-Control: public, max-age=2592000
  Server: gws
  Content-Length: 219
  X-XSS-Protection: 1; mode=block
  X-Frame-Options: SAMEORIGIN
Location: http://www.google.com/ [following]
--2012-08-25 12:20:29--  http://www.google.com/
Resolving www.google.com (www.google.com)... 173.194.69.99, 173.194.69.104, 173.194.69.106, ...

  ...skipped a few more redirections ...

    [<=>                                                                                                                                     ] 0           --.-K/s              
<!doctype html><html itemscope="itemscope" itemtype="http://schema.org/WebPage"><head><meta itemprop="image" content="/images/google_favicon_128.png"><ti 

... skipped ...
Run Code Online (Sandbox Code Playgroud)

也许你需要更新你的wget(~$ wget --version GNU Wget 1.14 built on linux-gnu.)


Abh*_*arn 11

这对我有用,用于打印带有标题的响应:

wget --server-response http://www.example.com/
Run Code Online (Sandbox Code Playgroud)

  • 您可能需要添加 `--spider` 参数。这个有用的原因是不要下载任何页面内容。 (2认同)

小智 6

这将不起作用:

wget -q -S -O - google.com 1>wget.txt 2>&1
Run Code Online (Sandbox Code Playgroud)

由于重定向是从右到左计算的,这会将 html 发送到 wget.txt 并将标头发送到 STDOUT:

wget -q -S -O - google.com 2>&1 1>wget.txt
Run Code Online (Sandbox Code Playgroud)