wget —— 下载、断点续传与站点镜像

运维工具手册 03-文件与传输 4.9 KB 原文

wget —— 下载、断点续传与站点镜像

分类:03 文件与传输 适用:全平台(GNU Wget 1.x) 一句话:比 curl 更"下载器",天生支持断点续传、递归抓取、后台运行。

1. 它解决什么问题

  • 网络不稳,下载断了要能接着下。
  • 一次性下载一批 URL。
  • 镜像一个静态站点/文档目录做离线备份。
  • 在只有 wget 没有 curl 的精简系统里做 HTTP 探测。

2. 与 curl 的分工

场景 选
调试接口、看请求响应细节、构造复杂请求 curl
大文件下载、断点续传、批量、递归镜像 wget
需要 POST/PUT、自定义方法、上传 curl
只需要"把文件拿下来" wget

3. 基础用法

wget https://example.com/file.tar.gz            # 下载到当前目录
wget -O out.tar.gz https://example.com/f.gz     # 指定文件名
wget -P /data/downloads https://example.com/f   # 指定目录
wget -c https://example.com/big.iso             # 断点续传(最常用)
wget -q https://example.com/f                   # 静默
wget -q --show-progress https://example.com/f   # 静默但显示进度条
wget -b https://example.com/big.iso             # 后台下载,日志 wget-log
wget -i urls.txt                                # 从文件批量下载
wget --spider https://example.com               # 只探测,不下载

4. 下载控制

wget --limit-rate=2m https://example.com/big.iso     # 限速 2MB/s
wget --tries=5 --timeout=30 --waitretry=5 <url>      # 重试与超时
wget --retry-connrefused <url>                       # 连接被拒也重试
wget -T 10 -t 3 <url>                                # 简写:超时 10s,重试 3 次
wget --no-check-certificate https://self-signed/f     # 跳过证书(仅测试)
wget --user=u --password=p https://example.com/f     # Basic 认证
wget --header='Authorization: Bearer x' <url>        # 自定义头
wget --user-agent='Mozilla/5.0' <url>                # 伪装 UA(部分站点拦 wget)
wget --post-data='a=1&b=2' <url>                     # POST
wget --method=PUT --body-file=/tmp/data <url>        # 其他方法
wget -4 / -6 <url>                                   # 强制 IPv4/IPv6
wget -e use_proxy=no <url>                           # 不走代理

5. 递归与镜像

# 抓取整个目录(同站,不向上爬)
wget -r -np -nH --cut-dirs=2 -R 'index.html*' https://example.com/docs/

# 完整镜像(含页面所需资源,转绝对链接)
wget -m -k -p https://example.com/

# 只抓某类文件
wget -r -np -A '*.pdf,*.zip' https://example.com/files/

# 排除某类
wget -r -np -R '*.mp4,*.iso' https://example.com/
参数 含义
-r 递归
-np 不追父目录(必加,否则会爬整个站)
-nH 不创建主机名目录
--cut-dirs=N 去掉前 N 层目录
-m 镜像(= -r -N -l inf --no-remove-listing)
-k 转换链接为本地相对路径
-p 下载页面所需资源(图片/CSS/JS)
-A / -R 接受 / 拒绝列表
-l N 递归深度
-D 限定域名
--wait=1 每次请求间隔,礼貌抓取
-e robots=off 忽略 robots.txt(确认有授权再用)

镜像他人站点前确认授权与 robots 协议。高频抓取等同于压测,容易封 IP。

6. 输出与日志

wget -o /var/log/wget.log <url>        # 日志写文件
wget -a /var/log/wget.log <url>        # 追加
wget -nv <url>                          # 精简输出
wget -d <url>                           # debug(排查代理/证书)

退出码(脚本判断用):

码 含义
0 成功
1 通用错误
2 命令行参数错误
3 文件 IO 错误
4 网络失败
5 SSL 校验失败
6 认证失败
7 协议错误
8 服务器返回错误(4xx/5xx)
wget -q <url> || echo "wget failed: $?"

7. 实战场景

7.1 内网批量拉包

cat > urls.txt <<'EOF'
https://example.com/a.rpm
https://example.com/b.rpm
https://example.com/c.rpm
EOF

wget -c -P /opt/packages -i urls.txt --tries=3 --timeout=20

7.2 离线文档镜像

wget -m -k -p -np --wait=0.5 \
  --user-agent='Mozilla/5.0' \
  https://docs.example.com/guide/

抓完把目录打包,内网可直接打开 index.html。

7.3 判断 URL 是否可达(不下载)

wget --spider -S https://example.com/api/health 2>&1 | grep 'HTTP/'
# 输出 HTTP/1.1 200 OK 即通

-S 打印响应头,比 curl -I 更"wget 风格"。

7.4 代理环境下载

export https_proxy=http://proxy.corp:8080
export http_proxy=http://proxy.corp:8080
export no_proxy=localhost,127.0.0.1,10.0.0.0/8
wget -c https://example.com/file

或在 ~/.wgetrc 里固化:

https_proxy = http://proxy.corp:8080
use_proxy = on
timeout = 30
tries = 3

7.5 校验下载完整性

wget -c https://example.com/app.tar.gz
wget -q https://example.com/app.tar.gz.sha256
sha256sum -c app.tar.gz.sha256

8. 常见坑

  1. -O 后接多个 URL 会覆盖:最后一个覆盖前面所有,批量下载要用 -P 或 --content-disposition。
  2. 递归不加 -np 会爬全站:可能几小时不停,甚至把对方打挂。
  3. -c 与 -O 同用有风险:文件名变化时续传会拼坏文件。
  4. 服务器不支持 Range 时 -c 无效:会从头下,日志里有提示。
  5. HTTPS 证书过期直接失败:--no-check-certificate 只是临时手段,应修证书。
  6. 默认 UA 是 Wget/x.x:很多站点直接 403,需要 --user-agent。
  7. -b 后台下载日志在 wget-log,当前目录写满会导致失败。
  8. -i 文件里含空行/注释:空行会被当 URL 报错,提前清理。
  9. 中文文件名乱码:服务器未按 RFC 5987 编码时会乱,需 --restrict-file-names=nocontrol 或手工改名。
  10. -m 会下载 .listing 等隐藏文件,注意清理。
  11. 走代理时内网地址也走代理:配好 no_proxy,否则内网下载全失败。

9. 速查表

wget <url>                        # 下载
wget -c <url>                     # 续传
wget -O name <url>                # 指定名
wget -P /dir <url>                # 指定目录
wget -q --show-progress <url>     # 安静+进度
wget -b <url>                     # 后台
wget -i urls.txt                  # 批量
wget --spider -S <url>            # 只探测
wget -r -np -nH -k -p <url>       # 抓目录
wget -m -k <url>                  # 镜像
wget --limit-rate=2m -c <url>     # 限速续传
wget --tries=3 --timeout=20 <url> # 重试
wget -e use_proxy=no <url>        # 不走代理
wget —— 下载、断点续传与站点镜像 - 知识库 - 起航社区