wget-Linux文件下载管理器

使用wget下载文件或网页

使用wget下载文件或网页,只需要提供文件或网页的URL。它将以默认原始名称下载文件到当前命令执行的目录中。

wget URL

如果提供一个默认为 index.html 的 URL,那么就会下载该索引页。默认情况下,文件会被下载到当前的工作目录,并保持原来的名字。

[root@rhce ~]# wget http://www.bunian.cn
--2022-06-05 16:34:50--  http://www.bunian.cn/
Resolving www.bunian.cn (www.bunian.cn)... 49.232.217.22
Connecting to www.bunian.cn (www.bunian.cn)|49.232.217.22|:80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: https://www.bunian.cn/ [following]
--2022-06-05 16:34:50--  https://www.bunian.cn/
Connecting to www.bunian.cn (www.bunian.cn)|49.232.217.22|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html]
Saving to: ‘index.html’

index.html                              [ <=>                                                                ] 162.22K   824KB/s    in 0.2s    

2022-06-05 16:34:51 (824 KB/s) - ‘index.html’ saved [166112]

通过使用 –output-document 和 – 符号,可以指示 wget 将数据发送到 标准输出(stdout):

[root@rhce ~]# wget https://www.bunian.cn --output-document - | head -n4
--2022-06-05 16:35:58--  https://www.bunian.cn/
Resolving www.bunian.cn (www.bunian.cn)... 49.232.217.22
Connecting to www.bunian.cn (www.bunian.cn)|49.232.217.22|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html]
Saving to: ‘STDOUT’

-                                       [<=>                                                                 ]       0  --.-KB/s               <!DOCTYPE HTML>
<html lang="zh-CN">
<head>
        <meta charset="UTF-8">
-                                       [ <=>                                                                ]  15.70K  --.-KB/s    in 0.2s    


Cannot write to ‘-’ (Broken pipe).

可以使用 –output-document 选项(简写为 -O)将下载文件命名为任何想要的名称:

[root@rhce ~]# wget https://www.bunian.cn --output-document bunian.html
--2022-06-05 16:40:14--  https://www.bunian.cn/
Resolving www.bunian.cn (www.bunian.cn)... 49.232.217.22
Connecting to www.bunian.cn (www.bunian.cn)|49.232.217.22|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html]
Saving to: ‘bunian.html’

bunian.html                             [ <=>                                                                ] 162.07K   819KB/s    in 0.2s    

2022-06-05 16:40:16 (819 KB/s) - ‘bunian.html’ saved [165964]

使用wget下载多个文件:

要下载多个文件,必须将它们的URL保存在一个文本文件中,并提供该文本文件作为wget的输入,如下所示:

[root@rhce ~]# wget -i download_files.txt

使用wget下载名称不同的文件:

网页几乎总是以index.html的形式保存在wget中。为下载的文件提供自定义名称。可以使用-O(大写字母O)选项来提供下载时的输出文件名。

[root@rhce ~]# wget -O filename URL

使用wget下载文件夹:

浏览FTP服务器,并且需要下载整个目录,则可以使用递归选项

[root@rhce ~]# wget -r ftp://server-address.com/directory wget -O filename URL

使用wget下载整个网站:

可以用wget镜像整个网站。整个网站是指整个面向公众的网站结构。虽然可以直接使用mirror选项-m,但是最好添加:

  • –convert-links:转换链接,以便内部链接指向下载的资源而不是Web
  • –page-conditions:下载其他内容,例如样式表,以使页面在脱机状态下看起来更好
[root@rhce ~]# wget -m --convert-links --page-requisites website_addres

恢复未完成的下载:

如果因为某些原因按C键中止了下载,可以用选项-c恢复之前的下载。

[root@rhce ~]# wget -c

断点续传:

如果正在下载一个超大文件,可能会遇到中断下载的情况。使用 –continue(简写为 -c),wget 可以确定从文件的哪个位置开始继续下载。也就是说,下次在下载一个 4 GB 的 Linux 发行版 ISO 时,如果出现了错误导致中断,不必重新从头开始。

[root@rhce ~]# wget --continue https://example.com/linux-distro.iso

下载一系列的文件:

如果要下载的不是单个大文件,而是一系列的文件,wget 也能提供很好的帮助。假如知道要下载文件的路径以及文件名的通用范式,可以使用 Bash 语法指示一个数字范围的起始和终点来表示这一系列文件名:

[root@rhce ~]# wget https://www.bunian.cn/file_{1..4}.webp

镜像整个站点:

使用 –mirror 选项可以下载整个站点,包括它的目录结构。这与使用选项 –recursive –level inf –timestamping –no-remove-listing 的效果是一样的,该选项表明可以进行无限制的递归,得到指定域下的所有内容。但也可能会得到比预期多得多的内容,这取决于站点本身的老旧程度。

如果正在使用 wget 来打包整个站点,选项 –no-cookies –page-requisites –convert-links 非常有用,它可以确保打包的站点是全新且完整的,站点副本或多或少是自包含的(self-contained)。

修改HTML标头:

在计算机发送的通信报文里含有大量用于数据交换的元数据(metadata)。HTTP 标头是初始数据的组成部分。当浏览某个站点时,浏览器会发送 HTTP 请求标头。使用 –debug 选项可以查看 wget 为每个请求发送了什么样的标头信息:

[root@rhce ~]# wget --debug www.bunian.cn
DEBUG output created by Wget 1.19.5 on linux-gnu.

Reading HSTS entries from /root/.wget-hsts
URI encoding = ‘UTF-8’
URL transformed to HTTPS due to an HSTS policy
Converted file name 'index.html' (UTF-8) -> 'index.html' (UTF-8)
--2022-06-05 16:43:22--  https://www.bunian.cn/
Certificates loaded: 147
Resolving www.bunian.cn (www.bunian.cn)... 49.232.217.22
Caching www.bunian.cn => 49.232.217.22
Connecting to www.bunian.cn (www.bunian.cn)|49.232.217.22|:443... connected.
Created socket 3.
Releasing 0x000055bc87768620 (new refcount 1).

---request begin---
GET / HTTP/1.1
User-Agent: Wget/1.19.5 (linux-gnu)
Accept: */*
Accept-Encoding: identity
Host: www.bunian.cn
Connection: Keep-Alive

---request end---
HTTP request sent, awaiting response... 
---response begin---
HTTP/1.1 200 OK
Server: nginx
Date: Sun, 05 Jun 2022 08:43:22 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
Link: <https://www.bunian.cn/wp-json/>; rel="https://api.w.org/"
Strict-Transport-Security: max-age=31536000

---response end---
200 OK
Registered socket 3 for persistent reuse.
Parsed Strict-Transport-Security max-age = 31536000, includeSubDomains = false
Updated HSTS host: www.bunian.cn:443 (max-age: 31536000, includeSubdomains: false)
URI content encoding = ‘UTF-8’
Length: unspecified [text/html]
Saving to: ‘index.html.1’

index.html.1                            [   <=>                                                              ] 162.15K   273KB/s    in 0.6s    

2022-06-05 16:43:24 (273 KB/s) - ‘index.html.1’ saved [166045]

Saving HSTS entries to /root/.wget-hsts

可以使用 –header 选项修改请求标头。实际上经常使用这种方式来模仿某特定浏览器,来测试或兼容某些编码糟糕、只能与特定代理通信的站点。

让请求被识别为来自 Windows 系统的 Microsoft Edge:

[root@rhce ~]# wget --debug --header="User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36 Edg/91.0.864.59" https://www.bunian.cn

也可以假装为某个移动设备:

[root@rhce ~]# wget --debug --header="User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 13_5_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.1 Mobile/15E148 Safari/604.1" https://www.bunian.cn

查看响应标头:

与浏览器发送请求标头的方式一样,响应也包含有标头信息。可以使用 –debug 选项来查看响应中的标头信息:

[root@rhce ~]# wget --debug www.bunian.cn
DEBUG output created by Wget 1.19.5 on linux-gnu.

Reading HSTS entries from /root/.wget-hsts
URI encoding = ‘UTF-8’
URL transformed to HTTPS due to an HSTS policy
Converted file name 'index.html' (UTF-8) -> 'index.html' (UTF-8)
--2022-06-05 16:46:24--  https://www.bunian.cn/
Certificates loaded: 147
Resolving www.bunian.cn (www.bunian.cn)... 49.232.217.22
Caching www.bunian.cn => 49.232.217.22
Connecting to www.bunian.cn (www.bunian.cn)|49.232.217.22|:443... connected.
Created socket 3.
Releasing 0x000055e2f7b1f620 (new refcount 1).

---request begin---
GET / HTTP/1.1
User-Agent: Wget/1.19.5 (linux-gnu)
Accept: */*
Accept-Encoding: identity
Host: www.bunian.cn
Connection: Keep-Alive

---request end---
HTTP request sent, awaiting response... 
---response begin---
HTTP/1.1 200 OK
Server: nginx
Date: Sun, 05 Jun 2022 08:46:24 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
Link: <https://www.bunian.cn/wp-json/>; rel="https://api.w.org/"
Strict-Transport-Security: max-age=31536000

---response end---
200 OK
Registered socket 3 for persistent reuse.
Parsed Strict-Transport-Security max-age = 31536000, includeSubDomains = false
Updated HSTS host: www.bunian.cn:443 (max-age: 31536000, includeSubdomains: false)
URI content encoding = ‘UTF-8’
Length: unspecified [text/html]
Saving to: ‘index.html.2’

index.html.2                            [    <=>                                                             ] 161.82K   218KB/s    in 0.7s    

2022-06-05 16:46:26 (218 KB/s) - ‘index.html.2’ saved [165708]

Saving HSTS entries to /root/.wget-hsts

处理301响应:

200 响应码意味着一切都在预料之中。而 301 响应则表示 URL 已经被永久迁移到了另外一个地方。这是站点管理员的一种常用手段,内容迁移后,为访问旧地址的用户留下寻找新地址的“线索”。wget 会默认跟随重定向(redirect),这也是大部分情况下用户所希望的。

当然,可以使用 –max-redirect 选项,用于控制 wget 默认处理 301 响应重定向的次数。设置为 0 意味着不会自动重定向到新的地址:

[root@rhce ~]# wget --max-redirect 0 https://www.bunian.cn
--2022-06-05 16:47:56--  https://www.bunian.cn/
Resolving www.bunian.cn (www.bunian.cn)... 49.232.217.22
Connecting to www.bunian.cn (www.bunian.cn)|49.232.217.22|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html]
Saving to: ‘index.html.3’

index.html.3                            [   <=>                                                              ] 162.26K   245KB/s    in 0.7s    

2022-06-05 16:47:58 (245 KB/s) - ‘index.html.3’ saved [166157]

同时,也可以设置为其他的数值来控制 wget 能重定向多少次。

展开URL缩写:

使用–max-redirect 选项用于在实际访问之前查看 URL 缩写(shortened URL)非常有用。缩写 URL 可用于用户无法完整拷贝和粘贴一个长 URL 时的印刷媒体(print media),或是具有字数限制的社交网络(在类似 Mastondon opensource.com 这种现代开源的社交网络上这并不是一个问题)。这种缩写具有一定的风险,因为本质上这些目的地是隐藏的。组合使用 –head 选项和 –location 选项来来查看 HTTP 头部并解开最终的目的地,可以在不加载整个资源的情况下查看到缩写 URL 的完整内容:

[root@rhce ~]# wget --max-redirect 0 "https://bunian.cn/20220605"
--2022-06-05 16:54:24--  https://bunian.cn/20220605
Resolving bunian.cn (bunian.cn)... 49.232.217.22
Connecting to bunian.cn (bunian.cn)|49.232.217.22|:443... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: https://www.bunian.cn/20220605 [following]
0 redirections exceeded.

从 Location 开始的倒数第二行输出,展示了实际的目的地。

© 版权声明
THE END
喜欢就支持一下吧
点赞142赞赏 分享
评论 抢沙发
头像
欢迎光临不念博客,留下您的想法和建议,祝您有愉快的一天~
提交
头像

昵称

取消
昵称代码图片

    暂无评论内容