wget的使用详解

我在工作中, 经常下载遥感影像,每个影像都很大,使用普通的c#编写的ftphelpe下载不太稳定(网络太差了),最终选择了linux下一款牛逼的下载工具wget,使用它的windows移植版本的。在此写此文,希望对和我一样保证数据下载完整的朋友们提供一个思路。

参考文档

http://man.linuxde.net/wget

http://gnuwin32.sourceforge.net/packages/wget.htm

https://wiki.archlinux.org/index.php/Wget

https://eternallybored.org/misc/wget/

https://eternallybored.org/misc/wget/manual/wget-1.16.2.html

什么是wget

wget是一个从网络上自动下载文件的自由工具,支持通过HTTP、HTTPS、FTP三个最常见的TCP/IP下载,并可以使用HTTP代理。wget名称的由来是“World Wide Web”与“get”的结合。

wget的获取

linux系统 默认就安装有wget工具, 如果没有,使用yum install wget即可(前提配置正确的yum源,不会的可以参照我的另外一篇博客http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_linux_002.html)
windows系统 可以百度wget for windows 获取, 这里提供一个地址:http://gnuwin32.sourceforge.net/packages/wget.htm,进入页面,找到那个download下的Binaries后的bin下载即可。这里提供一个1.11.4的下载地址:http://downloads.sourceforge.net/gnuwin32/wget-1.11.4-1-bin.zip

注意有时候需要请求带有认证的https地址的时候,可能需要wget的版本高于1.8的。从这个地址找对应的版本:  https://eternallybored.org/misc/wget/

wget的好处

  •      可以使用REST和RANGE恢复已中止的下载
  •      可以使用文件名通配符和递归镜像目录
  •      许多不同语言的基于NLS的消息文件
  •      可选地,将下载的文档中的绝对链接转换为相对链接,以便下载的文档可以在本地链接到彼此
  •      在大多数类UNIX操作系统以及Microsoft Windows上运行
  •      支持HTTP代理
  •      支持HTTP cookie
  •      支持持久HTTP连接
  •      无人值守/后台操作
  •      使用本地文件时间戳来确定是否需要在镜像时重新下载文档
  •      GNU Wget是根据GNU通用公共许可证分发的。

wget怎么用:

  用法: wget [选项]... [URL]...

wget(选项)(参数)

来自: http://man.linuxde.net/wget
wget(选项)(参数)

来自: http://man.linuxde.net/wget

wget常用选项

  •  -a<日志文件>:在指定的日志文件中记录资料的执行过程;
  •  -A<后缀名>:指定要下载文件的后缀名,多个后缀名之间使用逗号进行分隔;
  •  -b:进行后台的方式运行wget;
  •  -B<连接地址>:设置参考的连接地址的基地地址;
  •  -c:继续执行上次终端的任务;
  •  -C<标志>:设置服务器数据块功能标志on为激活,off为关闭,默认值为on;
  •  -d:调试模式运行指令;
  •  -D<域名列表>:设置顺着的域名列表,域名之间用“,”分隔;
  •  -e<指令>:作为文件“.wgetrc”中的一部分执行指定的指令;
  •  -h:显示指令帮助信息;
  •  -i<文件>:从指定文件获取要下载的URL地址;
  •  -l<目录列表>:设置顺着的目录列表,多个目录用“,”分隔;
  •  -L:仅顺着关联的连接;
  •  -r:递归下载方式;
  •  -nc:文件存在时,下载文件不覆盖原有文件;
  •  -nv:下载时只显示更新和出错信息,不显示指令的详细执行过程;
  •  -q:不显示指令执行过程;
  •  -nh:不查询主机名称;
  •  -v:显示详细执行过程;
  •  -V:显示版本信息;
  •  --passive-ftp:使用被动模式PASV连接FTP服务器;
  •  --follow-ftp:从HTML文件中下载FTP连接文件。
-a<日志文件>:在指定的日志文件中记录资料的执行过程; -A<后缀名>:指定要下载文件的后缀名,多个后缀名之间使用逗号进行分隔; -b:进行后台的方式运行wget; -B<连接地址>:设置参考的连接地址的基地地址; -c:继续执行上次终端的任务; -C<标志>:设置服务器数据块功能标志on为激活,off为关闭,默认值为on; -d:调试模式运行指令; -D<域名列表>:设置顺着的域名列表,域名之间用“,”分隔; -e<指令>:作为文件“.wgetrc”中的一部分执行指定的指令; -h:显示指令帮助信息; -i<文件>:从指定文件获取要下载的URL地址; -l<目录列表>:设置顺着的目录列表,多个目录用“,”分隔; -L:仅顺着关联的连接; -r:递归下载方式; -nc:文件存在时,下载文件不覆盖原有文件; -nv:下载时只显示更新和出错信息,不显示指令的详细执行过程; -q:不显示指令执行过程; -nh:不查询主机名称; -v:显示详细执行过程; -V:显示版本信息; --passive-ftp:使用被动模式PASV连接FTP服务器; --follow-ftp:从HTML文件中下载FTP连接文件。

来自: http://man.linuxde.net/wget

wget所有选项

windows 环境:可以使用 C:UsersAdministratorDownloadswget-1.11.4-1-binin>wget --help >> wgetHelp.txt 这个命令 wget的帮助信息重定向到wgetHelp.txt中去详细查看。

linux环境:可以使用man wget 查看帮助信息。

GNU Wget 1.11.4,非交互式的网络文件下载工具。
用法: wget [选项]... [URL]...

Mandatory arguments to long options are mandatory for short options too.

Startup:
  -V,  --version           display the version of Wget and exit.
  -h,  --help              print this help.
  -b,  --background        go to background after startup.
  -e,  --execute=COMMAND   execute a `.wgetrc'-style command.

Logging and input file:
  -o,  --output-file=FILE    log messages to FILE.
  -a,  --append-output=FILE  append messages to FILE.
  -d,  --debug               print lots of debugging information.
  -q,  --quiet               quiet (no output).
  -v,  --verbose             be verbose (this is the default).
  -nv, --no-verbose          turn off verboseness, without being quiet.
  -i,  --input-file=FILE     download URLs found in FILE.
  -F,  --force-html          treat input file as HTML.
  -B,  --base=URL            prepends URL to relative links in -F -i file.

Download:
  -t,  --tries=NUMBER            set number of retries to NUMBER (0 unlimits).
       --retry-connrefused       retry even if connection is refused.
  -O,  --output-document=FILE    write documents to FILE.
  -nc, --no-clobber              skip downloads that would download to
                                 existing files.
  -c,  --continue                resume getting a partially-downloaded file.
       --progress=TYPE           select progress gauge type.
  -N,  --timestamping            don't re-retrieve files unless newer than
                                 local.
  -S,  --server-response         print server response.
       --spider                  don't download anything.
  -T,  --timeout=SECONDS         set all timeout values to SECONDS.
       --dns-timeout=SECS        set the DNS lookup timeout to SECS.
       --connect-timeout=SECS    set the connect timeout to SECS.
       --read-timeout=SECS       set the read timeout to SECS.
  -w,  --wait=SECONDS            wait SECONDS between retrievals.
       --waitretry=SECONDS       wait 1..SECONDS between retries of a retrieval.
       --random-wait             wait from 0...2*WAIT secs between retrievals.
       --no-proxy                explicitly turn off proxy.
  -Q,  --quota=NUMBER            set retrieval quota to NUMBER.
       --bind-address=ADDRESS    bind to ADDRESS (hostname or IP) on local host.
       --limit-rate=RATE         limit download rate to RATE.
       --no-dns-cache            disable caching DNS lookups.
       --restrict-file-names=OS  restrict chars in file names to ones OS allows.
       --ignore-case             ignore case when matching files/directories.
  -4,  --inet4-only              connect only to IPv4 addresses.
  -6,  --inet6-only              connect only to IPv6 addresses.
       --prefer-family=FAMILY    connect first to addresses of specified family,
                                 one of IPv6, IPv4, or none.
       --user=USER               set both ftp and http user to USER.
       --password=PASS           set both ftp and http password to PASS.

Directories:
  -nd, --no-directories           don't create directories.
  -x,  --force-directories        force creation of directories.
  -nH, --no-host-directories      don't create host directories.
       --protocol-directories     use protocol name in directories.
  -P,  --directory-prefix=PREFIX  save files to PREFIX/...
       --cut-dirs=NUMBER          ignore NUMBER remote directory components.

HTTP options:
       --http-user=USER        set http user to USER.
       --http-password=PASS    set http password to PASS.
       --no-cache              disallow server-cached data.
  -E,  --html-extension        save HTML documents with `.html' extension.
       --ignore-length         ignore `Content-Length' header field.
       --header=STRING         insert STRING among the headers.
       --max-redirect          maximum redirections allowed per page.
       --proxy-user=USER       set USER as proxy username.
       --proxy-password=PASS   set PASS as proxy password.
       --referer=URL           include `Referer: URL' header in HTTP request.
       --save-headers          save the HTTP headers to file.
  -U,  --user-agent=AGENT      identify as AGENT instead of Wget/VERSION.
       --no-http-keep-alive    disable HTTP keep-alive (persistent connections).
       --no-cookies            don't use cookies.
       --load-cookies=FILE     load cookies from FILE before session.
       --save-cookies=FILE     save cookies to FILE after session.
       --keep-session-cookies  load and save session (non-permanent) cookies.
       --post-data=STRING      use the POST method; send STRING as the data.
       --post-file=FILE        use the POST method; send contents of FILE.
       --content-disposition   honor the Content-Disposition header when
                               choosing local file names (EXPERIMENTAL).
       --auth-no-challenge     Send Basic HTTP authentication information
                               without first waiting for the server's
                               challenge.

HTTPS (SSL/TLS) options:
       --secure-protocol=PR     choose secure protocol, one of auto, SSLv2,
                                SSLv3, and TLSv1.
       --no-check-certificate   don't validate the server's certificate.
       --certificate=FILE       client certificate file.
       --certificate-type=TYPE  client certificate type, PEM or DER.
       --private-key=FILE       private key file.
       --private-key-type=TYPE  private key type, PEM or DER.
       --ca-certificate=FILE    file with the bundle of CA's.
       --ca-directory=DIR       directory where hash list of CA's is stored.
       --random-file=FILE       file with random data for seeding the SSL PRNG.
       --egd-file=FILE          file naming the EGD socket with random data.

FTP options:
       --ftp-user=USER         set ftp user to USER.
       --ftp-password=PASS     set ftp password to PASS.
       --no-remove-listing     don't remove `.listing' files.
       --no-glob               turn off FTP file name globbing.
       --no-passive-ftp        disable the "passive" transfer mode.
       --retr-symlinks         when recursing, get linked-to files (not dir).
       --preserve-permissions  preserve remote file permissions.

Recursive download:
  -r,  --recursive          specify recursive download.
  -l,  --level=NUMBER       maximum recursion depth (inf or 0 for infinite).
       --delete-after       delete files locally after downloading them.
  -k,  --convert-links      make links in downloaded HTML point to local files.
  -K,  --backup-converted   before converting file X, back up as X.orig.
  -m,  --mirror             shortcut for -N -r -l inf --no-remove-listing.
  -p,  --page-requisites    get all images, etc. needed to display HTML page.
       --strict-comments    turn on strict (SGML) handling of HTML comments.

Recursive accept/reject:
  -A,  --accept=LIST               comma-separated list of accepted extensions.
  -R,  --reject=LIST               comma-separated list of rejected extensions.
  -D,  --domains=LIST              comma-separated list of accepted domains.
       --exclude-domains=LIST      comma-separated list of rejected domains.
       --follow-ftp                follow FTP links from HTML documents.
       --follow-tags=LIST          comma-separated list of followed HTML tags.
       --ignore-tags=LIST          comma-separated list of ignored HTML tags.
  -H,  --span-hosts                go to foreign hosts when recursive.
  -L,  --relative                  follow relative links only.
  -I,  --include-directories=LIST  list of allowed directories.
  -X,  --exclude-directories=LIST  list of excluded directories.
  -np, --no-parent                 don't ascend to the parent directory.

请将错误报告或建议寄给 <bug-wget@gnu.org>。

翻译后就是下面的啦:

启动:
  -V,  --version           显示 Wget 的版本信息并退出。
  -h,  --help              打印此帮助。
  -b,  --background        启动后转入后台。
  -e,  --execute=COMMAND   运行一个“.wgetrc”风格的命令。

日志和输入文件:
  -o,  --output-file=FILE    将日志信息写入 FILE。
  -a,  --append-output=FILE  将信息添加至 FILE。
  -d,  --debug               打印大量调试信息。
  -q,  --quiet               安静模式 (无信息输出)。
  -v,  --verbose             详尽的输出 (此为默认值)。
  -nv, --no-verbose          关闭详尽输出,但不进入安静模式。
  -i,  --input-file=FILE     下载本地或外部 FILE 中的 URLs。
  -F,  --force-html          把输入文件当成 HTML 文件。
  -B,  --base=URL            解析与 URL 相关的
                             HTML 输入文件 (由 -i -F 选项指定)。
       --config=FILE         Specify config file to use.

下载:
  -t,  --tries=NUMBER            设置重试次数为 NUMBER (0 代表无限制)。
       --retry-connrefused       即使拒绝连接也是重试。
  -O,  --output-document=FILE    将文档写入 FILE。
  -nc, --no-clobber              skip downloads that would download to
                                 existing files (overwriting them).
  -c,  --continue                断点续传下载文件。
       --progress=TYPE           选择进度条类型。
  -N,  --timestamping            只获取比本地文件新的文件。
  --no-use-server-timestamps     不用服务器上的时间戳来设置本地文件。
  -S,  --server-response         打印服务器响应。
       --spider                  不下载任何文件。
  -T,  --timeout=SECONDS         将所有超时设为 SECONDS 秒。
       --dns-timeout=SECS        设置 DNS 查寻超时为 SECS 秒。
       --connect-timeout=SECS    设置连接超时为 SECS 秒。
       --read-timeout=SECS       设置读取超时为 SECS 秒。
  -w,  --wait=SECONDS            等待间隔为 SECONDS 秒。
       --waitretry=SECONDS       在获取文件的重试期间等待 1..SECONDS 秒。
       --random-wait             获取多个文件时,每次随机等待间隔
                                 0.5*WAIT...1.5*WAIT 秒。
       --no-proxy                禁止使用代理。
  -Q,  --quota=NUMBER            设置获取配额为 NUMBER 字节。
       --bind-address=ADDRESS    绑定至本地主机上的 ADDRESS (主机名或是 IP)。
       --limit-rate=RATE         限制下载速率为 RATE。
       --no-dns-cache            关闭 DNS 查寻缓存。
       --restrict-file-names=OS  限定文件名中的字符为 OS 允许的字符。
       --ignore-case             匹配文件/目录时忽略大小写。
  -4,  --inet4-only              仅连接至 IPv4 地址。
  -6,  --inet6-only              仅连接至 IPv6 地址。
       --prefer-family=FAMILY    首先连接至指定协议的地址
                                 FAMILY 为 IPv6,IPv4 或是 none。
       --user=USER               将 ftp 和 http 的用户名均设置为 USER。
       --password=PASS           将 ftp 和 http 的密码均设置为 PASS。
       --ask-password            提示输入密码。
       --no-iri                  关闭 IRI 支持。
       --local-encoding=ENC      IRI (国际化资源标识符) 使用 ENC 作为本地编码。
       --remote-encoding=ENC     使用 ENC 作为默认远程编码。
       --unlink                  remove file before clobber.

目录:
  -nd, --no-directories           不创建目录。
  -x,  --force-directories        强制创建目录。
  -nH, --no-host-directories      不要创建主目录。
       --protocol-directories     在目录中使用协议名称。
  -P,  --directory-prefix=PREFIX  以 PREFIX/... 保存文件
       --cut-dirs=NUMBER          忽略远程目录中 NUMBER 个目录层。

HTTP 选项:
       --http-user=USER        设置 http 用户名为 USER。
       --http-password=PASS    设置 http 密码为 PASS。
       --no-cache              不在服务器上缓存数据。
       --default-page=NAME     改变默认页
                               (默认页通常是“index.html”)。
  -E,  --adjust-extension      以合适的扩展名保存 HTML/CSS 文档。
       --ignore-length         忽略头部的‘Content-Length’区域。
       --header=STRING         在头部插入 STRING。
       --max-redirect          每页所允许的最大重定向。
       --proxy-user=USER       使用 USER 作为代理用户名。
       --proxy-password=PASS   使用 PASS 作为代理密码。
       --referer=URL           在 HTTP 请求头包含‘Referer: URL’。
       --save-headers          将 HTTP 头保存至文件。
  -U,  --user-agent=AGENT      标识为 AGENT 而不是 Wget/VERSION。
       --no-http-keep-alive    禁用 HTTP keep-alive (永久连接)。
       --no-cookies            不使用 cookies。
       --load-cookies=FILE     会话开始前从 FILE 中载入 cookies。
       --save-cookies=FILE     会话结束后保存 cookies 至 FILE。
       --keep-session-cookies  载入并保存会话 (非永久) cookies。
       --post-data=STRING      使用 POST 方式;把 STRING 作为数据发送。
       --post-file=FILE        使用 POST 方式;发送 FILE 内容。
       --content-disposition   当选中本地文件名时
                               允许 Content-Disposition 头部 (尚在实验)。
       --auth-no-challenge     发送不含服务器询问的首次等待
                               的基本 HTTP 验证信息。

HTTPS (SSL/TLS) 选项:
       --secure-protocol=PR     选择安全协议,可以是 auto、SSLv2、
                                SSLv3 或是 TLSv1 中的一个。
       --no-check-certificate   不要验证服务器的证书。
       --certificate=FILE       客户端证书文件。
       --certificate-type=TYPE  客户端证书类型,PEM 或 DER。
       --private-key=FILE       私钥文件。
       --private-key-type=TYPE  私钥文件类型,PEM 或 DER。
       --ca-certificate=FILE    带有一组 CA 认证的文件。
       --ca-directory=DIR       保存 CA 认证的哈希列表的目录。
       --random-file=FILE       带有生成 SSL PRNG 的随机数据的文件。
       --egd-file=FILE          用于命名带有随机数据的 EGD 套接字的文件。

FTP 选项:
       --ftp-user=USER         设置 ftp 用户名为 USER。
       --ftp-password=PASS     设置 ftp 密码为 PASS。
       --no-remove-listing     不要删除‘.listing’文件。
       --no-glob               不在 FTP 文件名中使用通配符展开。
       --no-passive-ftp        禁用“passive”传输模式。
       --retr-symlinks         递归目录时,获取链接的文件 (而非目录)。

递归下载:
  -r,  --recursive          指定递归下载。
  -l,  --level=NUMBER       最大递归深度 (inf 或 0 代表无限制,即全部下载)。
       --delete-after       下载完成后删除本地文件。
  -k,  --convert-links      让下载得到的 HTML 或 CSS 中的链接指向本地文件。
  -K,  --backup-converted   在转换文件 X 前先将它备份为 X.orig。
  -m,  --mirror             -N -r -l inf --no-remove-listing 的缩写形式。
  -p,  --page-requisites    下载所有用于显示 HTML 页面的图片之类的元素。
       --strict-comments    用严格方式 (SGML) 处理 HTML 注释。

递归接受/拒绝:
  -A,  --accept=LIST               逗号分隔的可接受的扩展名列表。
  -R,  --reject=LIST               逗号分隔的要拒绝的扩展名列表。
  -D,  --domains=LIST              逗号分隔的可接受的域列表。
       --exclude-domains=LIST      逗号分隔的要拒绝的域列表。
       --follow-ftp                跟踪 HTML 文档中的 FTP 链接。
       --follow-tags=LIST          逗号分隔的跟踪的 HTML 标识列表。
       --ignore-tags=LIST          逗号分隔的忽略的 HTML 标识列表。
  -H,  --span-hosts                递归时转向外部主机。
  -L,  --relative                  只跟踪有关系的链接。
  -I,  --include-directories=LIST  允许目录的列表。
  --trust-server-names             use the name specified by the redirection
                                   url last component.
  -X,  --exclude-directories=LIST  排除目录的列表。
  -np, --no-parent                 不追溯至父目录。

wget常用用法

 1.使用wget下载单个文件

wget http://dldir1.qq.com/qqfile/qq/QQ8.7/19113/QQ8.7.exe

2.下载并以不同的文件名保存

wget -O qq.exe  http://dldir1.qq.com/qqfile/qq/QQ8.7/19113/QQ8.7.exe

3.wget限速下载

wget --limit-rate=300k  http://dldir1.qq.com/qqfile/qq/QQ8.7/19113/QQ8.7.exe

4.使用wget后台下载

wget -b  http://dldir1.qq.com/qqfile/qq/QQ8.7/19113/QQ8.7.exe

5.使用wget断点续传

wget -c http://dldir1.qq.com/qqfile/qq/QQ8.7/19113/QQ8.7.exe

6.伪装代理名称下载

wget --user-agent="Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.204 Safari/534.16" http://dldir1.qq.com/qqfile/qq/QQ8.7/19113/QQ8.7.exe

7.测试下载链接

wget --spider http://dldir1.qq.com/qqfile/qq/QQ8.7/19113/QQ8.7.exe

8.增加重试次数

wget --tries=40 http://dldir1.qq.com/qqfile/qq/QQ8.7/19113/QQ8.7.exe

9.下载多个文件

wget -i filelist.txt 
其中filelist 里面都是url, 通常以行分割
增加重试次数

来自: http://man.linuxde.net/wget
使用wget下载单个文件

来自: http://man.linuxde.net/wget

10.镜像网站

wget --mirror -p --convert-links -P ./LOCAL URL 
 --miror开户镜像下载。
 -p下载所有为了html页面显示正常的文件。
 --convert-links下载后,转换成本地的链接。
 -P ./LOCAL保存所有文件和目录到本地指定目录

11.过滤指定格式下载

wget --reject=gif url

12.把下载信息存入日志文件

wget -o download.log http://dldir1.qq.com/qqfile/qq/QQ8.7/19113/QQ8.7.exe

13.下载指定格式文件

wget -r -A.pdf url

15.FTP下载

wget --ftp-user=USERNAME --ftp-password=PASSWORD url

其他

原文地址:https://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_linux_004.html