wget — 非互動式網路下載工具
從網路下載檔案,支援斷點續傳、批次下載與遞迴爬取。
語法
wget [選項] URL
常用選項
| 選項 | 說明 | 範例 |
|---|---|---|
-O 檔案 |
儲存為指定檔名 | wget -O myfile.tar.gz URL |
-c, --continue |
斷點續傳(繼續未完成的下載) | wget -c URL |
-b, --background |
在背景下載(輸出到 wget-log) | wget -b URL |
-q, --quiet |
靜默模式,不顯示進度 | wget -q URL |
-r, --recursive |
遞迴下載(爬取整個網站) | wget -r URL |
-np, --no-parent |
遞迴時不往上層路徑走 | wget -r -np URL |
-A 副檔名 |
只下載指定副檔名的檔案(搭配 -r 使用) | wget -r -A pdf URL |
-P 目錄 |
儲存到指定目錄 | wget -P ~/downloads/ URL |
--limit-rate=速率 |
限制下載速率(如 200k、1m) | wget --limit-rate=500k URL |
--user-agent=字串 |
設定 User-Agent | wget --user-agent="Mozilla/5.0" URL |
-i 檔案 |
從文字檔讀取 URL 清單批次下載 | wget -i urls.txt |
--retry-connrefused |
連線被拒絕時也重試 | wget --retry-connrefused URL |
-t N, --tries=N |
重試次數(預設 20,0=無限) | wget -t 5 URL |
使用範例
範例 1:下載單一檔案
最基本用法,以 URL 的原始檔名儲存到當前目錄。
$ wget https://example.com/files/software-1.0.tar.gz
Resolving example.com...
Connecting...
HTTP request sent, awaiting response... 200 OK
software-1.0.tar.gz [1.23 MB] saved
範例 2:儲存為自訂檔名
使用 -O 指定下載後儲存的本地檔名。
$ wget -O docker-install.sh https://get.docker.com
範例 3:斷點續傳
使用 -c 在下載中斷後繼續,不需重新開始。
$ wget -c https://example.com/large-dataset.tar.gz
Continuing in background, pid 12345.
Output will be written to 'wget-log'.
範例 4:批次下載
將多個 URL 寫入文字檔,用 -i 批次下載。
$ wget -i urls.txt -P ~/downloads/
範例 5:限速下載(不佔滿頻寬)
限制下載速率,避免佔用所有頻寬影響其他服務。
$ wget --limit-rate=2m -c https://example.com/large-file.iso
範例 6:下載整個目錄(遞迴)
遞迴下載指定目錄下的所有 PDF 檔案。
$ wget -r -np -A pdf https://example.com/docs/
常見錯誤與排錯
ERROR 403: Forbidden.
- 原因
- 伺服器拒絕 wget 的 User-Agent(wget 常被封鎖)。
- 解法
- 加上 --user-agent 模擬瀏覽器:wget --user-agent="Mozilla/5.0 ..." URL
ERROR: The certificate of 'site.com' is not trusted.
- 原因
- SSL 憑證問題(自簽憑證或憑證過期)。
- 解法
- 測試時加 --no-check-certificate(正式環境請修復憑證問題)。