時間:2023-04-24 10:33:01 | 來源:網(wǎng)站運營
時間:2023-04-24 10:33:01 來源:網(wǎng)站運營
wget命令8種實用用法:大家好,我是良許。wget
是一個可以從網(wǎng)絡(luò)上下載文件的免費實用程序,它的工作原理是從 Internet 上獲取數(shù)據(jù),并將其保存到本地文件中或顯示在你的終端上。wget
程序進(jìn)行數(shù)據(jù)下載。wget
命令常見使用方式,希望對小伙伴們有所幫助。wget
命令來下載指定鏈接的文件。默認(rèn)情況下,下載的文件將保存到當(dāng)前工作目錄中的同名文件中。$ wget http://www.lxlinux.net--2021-09-20 17:23:47-- http://www.lxlinux.net/Resolving www.lxlinux.net... 93.184.216.34, 2606:2800:220:1:248:1893:25c8:1946Connecting to www.lxlinux.net|93.184.216.34|:80... connected.HTTP request sent, awaiting response... 200 OKLength: 1256 (1.2K) [text/html]Saving to: 'index.html'
如果你不想將下載的文件保存在本地,而只是想將其顯示在標(biāo)準(zhǔn)輸出(stdout)里,可以使用 --output-document
選項,同時后面再跟上一個 -
號。$ wget http://www.lxlinux.net --output-document - | head -n4<!doctype html><html><head> <title>Example Domain</title>
如果你想給下載的文件重新命名,那么可以使用 --output-document
選項(或者更簡便,直接使用 -O
即可):$ wget http://www.lxlinux.net --output-document newfile.html
--continue
選項(或者 -c
)實現(xiàn)斷點續(xù)傳。也就是說,如果因為各種原因?qū)е孪螺d中斷,使用了這個選項,就可以繼續(xù)上次的下載,而不需要重新進(jìn)行下載。$ wget --continue https://www.lxlinux.net/linux-distro.iso
wget
命令也可以幫你輕松實現(xiàn)。$ wget http://www.lxlinux.net/file_{1..4}.txt
--mirror
選項。--recursive --level inf --timestamping --no-remove-listing
,這意味著它是無限遞歸的,因此你可以下載到指定域上的所有內(nèi)容。wget
存檔站點,那么這些選項 --no-cookies --page-requisites --convert-links
也可用于確保每個頁面都是最新的、最完整的。--debug
選項查看 wget
每次請求發(fā)送的標(biāo)頭信息:$ wget --debug www.lxlinux.net---request begin---GET / HTTP/1.1User-Agent: Wget/1.19.5 (linux-gnu)Accept: */*Accept-Encoding: identityHost: www.lxlinux.netConnection: Keep-Alive---request end---
你可以使用 --header
選項修改請求標(biāo)頭。為什么要這么做呢?其實有很多使用場景的。例如,有時候為了測試,需要模擬特定的瀏覽器發(fā)出的請求。$ 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" http://www.lxlinux.net
除此之外,你還可以偽裝成特定的移動設(shè)備(比如 iPhone ):$ 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" /HTTP:// www.lxlinux.net
--debug
選項查看響應(yīng)標(biāo)頭:$ wget --debug www.lxlinux.net[...]---response begin---HTTP/1.1 200 OKAccept-Ranges: bytesAge: 188102Cache-Control: max-age=604800Content-Type: text/html; charset=UTF-8Etag: "3147526947"Server: ECS (sab/574F)Vary: Accept-EncodingX-Cache: HITContent-Length: 1256---response end---200 OKRegistered socket 3 for persistent reuse.URI content encoding = 'UTF-8'Length: 1256 (1.2K) [text/html]Saving to: 'index.html'
wget
的重定向功能。所以,如果你遇到 301 響應(yīng)時,就需要使用 --max-redirect
選項。--max-redirect
設(shè)置為 0 。$ wget --max-redirect 0 http://www.lxlinux.net--2021-09-21 11:01:35-- http://www.lxlinux.net/Resolving www.lxlinux.net... 192.0.43.8, 2001:500:88:200::8Connecting to www.lxlinux.net|192.0.43.8|:80... connected.HTTP request sent, awaiting response... 301 Moved PermanentlyLocation: https://www.www.lxlinux.net/ [following]0 redirections exceeded.
或者,你還可以將其設(shè)置為其他數(shù)字以控制 wget
跟隨的重定向數(shù)量。wget
命令來將短鏈接還原為長鏈接。這里依然使用 --max-redirect
選項:$ wget --max-redirect 0 "https://bit.ly/2yDyS4T"--2021-09-21 11:32:04-- https://bit.ly/2yDyS4TResolving bit.ly... 67.199.248.10, 67.199.248.11Connecting to bit.ly|67.199.248.10|:443... connected.HTTP request sent, awaiting response... 301 Moved PermanentlyLocation: http://www.lxlinux.net/ [following]0 redirections exceeded.
輸出的倒數(shù)第二行,在 Location 部分,后面你就看到了短鏈展開之后的真面目。關(guān)鍵詞:實用,命令
微信公眾號
版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。