Linux Wget 输出文档和标头到 STDOUT

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12120935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:10:40  来源:igfitidea点击:

Wget output document and headers to STDOUT

linuxhttpwget

提问by user1239398

I'm trying to output document body and its headers to stdout with wget by wget -S -O - http://google.com

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

but it shows only html docment.

但它只显示 html 文档。

Thanks

谢谢

UPD:

更新:

Worked this wget --save-headers --output-document - http://google.com

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

wget --versionshows GNU Wget 1.11.4 Red Hat modified

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

回答by BeniBela

It works here:

它在这里工作:

    $ 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 ...

perhaps you need to update your wget (~$ wget --version GNU Wget 1.14 built on linux-gnu.)

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

回答by thkala

wget -S -O - http://google.comworks as expected for me, butwith a caveat: the headers are considered debugging information and as such they are sent to the standard errorrather than the standard output. If you are redirecting the standard output to a file or another process, you will only get the document contents.

wget -S -O - http://google.com对我来说按预期工作,有一个警告:标头被视为调试信息,因此它们被发送到标准错误而不是标准输出。如果您将标准输出重定向到文件或其他进程,您将只能获得文档内容。

You can try redirecting the standard error to the standard output as a possible solution. For example, in bash:

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

$ wget -q -S -O - 2>&1 | grep ...

or

或者

$ wget -q -S -O - 1>wget.txt 2>&1

The -qoption suppresses the progress bar and some other annoyingly chatty parts of the wgetoutput.

-q选项会抑制进度条和其他一些烦人的wget输出部分。

回答by Joseph Lust

Try the following, no extra headers

尝试以下,没有额外的标题

wget -qO- www.google.com

Note the trailing -. This is part of the normal command argument for -Oto cat out to a file, but since we don't use >to direct to a file, it goes out to the shell. You can use -qO-or -qO -.

注意尾随-. 这是用于-Ocat out 到文件的正常命令参数的一部分,但由于我们不习惯于>直接指向文件,因此它会输出到 shell。您可以使用-qO--qO -

回答by maniac_on_moon

This will not work:

这将不起作用:

wget -q -S -O - google.com 1>wget.txt 2>&1

since redirects are evaluated right to left, this sends html to wget.txt and the header to STDOUT:

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

wget -q -S -O - google.com 2>&1 1>wget.txt

回答by Abhishek saharn

This worked for me for printing response with header:

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

wget --server-response http://www.example.com/