1. 程式人生 > >Wget下載終極用法和15個詳細的例子

Wget下載終極用法和15個詳細的例子

Wget是一種很好用的因特網下載工具,他具有的很多特性是其他工具所不能比擬的,再者他是一個輕量級可配置的下載工具。

本文結合例子介紹Windows下wget的多種下載方法和用途。

1、用Wget下載單個檔案

下載的時候會顯示:

  ~檔案的大小、連線狀態、連線地址以及檔案的大小

  ~儲存的名稱

  ~下載進度條

  ~下載速度、時間,還有多少未下載

例如我下載editplus時輸入

D:\Hack stuff\wget>wget http://software-files-a.cnet.com/s/software/12/32/81/47/
epp331.exe?token=1329413178_4553efa847829f3ecef10c1bc256fcc0&lop
=link&ptype=3001
&ontid=2352&siteId=4&edId=3&spi=537d5d5485f688682d82c481c4fb15a1&pid=12328147&ps
id=10018241&&fileName=epp331.exe

則下載時會顯示以下內容

D:\Hack stuff\wget>wget http://software-files-a.cnet.com/s/software/12/32/81/47/
epp331.exe?token=1329413178_4553efa847829f3ecef10c1bc256fcc0&lop
=link&ptype=3001
&ontid=2352&siteId=4&edId=3&spi=537d5d5485f688682d82c481c4fb15a1&pid=12328147&ps
id=10018241&&fileName=epp331.exe
--2012-02-16 15:28:50-- http://software-files-a.cnet.com/s/software/12/32/81/47
/epp331.exe?token=1329413178_4553efa847829f3ecef10c1bc256fcc0
Resolving software-files-a.cnet.com... 204.2.171.33, 204.2.171.35
Connecting to software-files-a.cnet.com|204.2.171.33|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1766464 (1.7M) [application/octet-stream]
Saving to: `
[email protected]
=1329413178_4553efa847829f3ecef10c1bc256fcc0'

18% [======> ] 335,238 20.5K/s eta 64s


2、用Wget-O下載可以為下載的檔案指定另外一個名字

預設情況下wget會用最後的斜線後面的所有字元來命名下載下來的檔案,如上例所示儲存的檔名為

Saving to: `[email protected]=1329413178_4553efa847829f3ecef10c1bc256fcc0'

這不是我們所想要的,我們可以用-O選項來改變將檔案儲存為editplus.exe

D:\Hack stuff\wget>wget -O editplus.exe http://software-files-a.cnet.com/s/software/12/32/81/47/
epp331.exe?token=1329413178_4553efa847829f3ecef10c1bc256fcc0&lop=link&ptype=3001
&ontid=2352&siteId=4&edId=3&spi=537d5d5485f688682d82c481c4fb15a1&pid=12328147&ps
id=10018241&&fileName=epp331.exe

3、用Wget --limit-rate指定下載的速度

如下面這個例子限制速度為300k

D:\Hack stuff\wget>wget --limit-rate=300k http://downloads.sourceforge.net/project/boost/boost-doc
s/1.47.0/boost_1_47_pdf.zip?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Fboost%2F
&ts=1329379231&use_mirror=nchc

4、續傳下載用Wget -c

當你在下載一個大檔案時突然中斷了那麼這個選項就派上用場了

D:\Hack stuff\wget>wget -c http://downloads.sourceforge.net/project/boost/boost-doc
s/1.47.0/boost_1_47_pdf.zip?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Fboost%2F
&ts=1329379231&use_mirror=nchc

5、後臺下載用wget -b

用此選項下載時只會初始化下載而不會顯示相關資訊

D:\Hack stuff\wget>wget -b http://downloads.sourceforge.net/project/boost/boost-
docs/1.47.0/boost_1_47_pdf.zip?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Fboost
%2F&ts=1329379231&use_mirror=nchc
Continuing in background, pid 6132.
Output will be written to `wget-log'.

下載以後會在wget目錄下生產wget-log檔案,用記事本開啟可檢視裡面的內容如下所示

--2012-02-16 16:12:55--  http://downloads.sourceforge.net/project/boost/boost-docs/1.47.0/boost_1_47_pdf.zip?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Fboost%2F
Resolving downloads.sourceforge.net... 216.34.181.59
Connecting to downloads.sourceforge.net|216.34.181.59|:80... connected.
HTTP request sent, awaiting response... 302 Found
Location: http://nchc.dl.sourceforge.net/project/boost/boost-docs/1.47.0/boost_1_47_pdf.zip [following]
--2012-02-16 16:12:56-- http://nchc.dl.sourceforge.net/project/boost/boost-docs/1.47.0/boost_1_47_pdf.zip
Resolving nchc.dl.sourceforge.net... 211.79.60.17
Connecting to nchc.dl.sourceforge.net|211.79.60.17|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 31421410 (30M) [application/zip]
Saving to: `boost_1_47_pdf.zip.4'

0K .......... .......... .......... .......... .......... 0% 19.7K 25m51s
50K .......... .......... .......... .......... .......... 0% 29.1K 21m40s
100K .......... .......... .......... .......... .......... 0% 20.8K 22m35s
150K .......... .......... .......... .......... .......... 0% 19.5K 23m26s
200K .......... .......... .......... .......... .......... 0% 18.4K 24m13s
250K .......... .......... .......... .......... .......... 0% 20.8K 24m13s
300K .......... .......... .......... .......... .......... 1% 18.2K 24m41s
350K .......... .......... .......... .......... .......... 1% 23.5K 24m16s


6、測試你要下載的地址用Wget --spider

wget --spider DOWNLOAD-URL

如果所給URL是正確的則會顯示

Resolving downloads.sourceforge.net... 216.34.181.59
Connecting to downloads.sourceforge.net|216.34.181.59|:80... connected.
HTTP request sent, awaiting response... 302 Found
Location: http://ncu.dl.sourceforge.net/project/boost/boost-docs/1.47.0/boost_1_
47_pdf.zip [following]
Spider mode enabled. Check if remote file exists.
--2012-02-16 16:21:08-- http://ncu.dl.sourceforge.net/project/boost/boost-docs/
1.47.0/boost_1_47_pdf.zip
Resolving ncu.dl.sourceforge.net... 140.115.17.45
Connecting to ncu.dl.sourceforge.net|140.115.17.45|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 31421410 (30M) [application/zip]
Remote file exists.

否則顯示

Spider mode enabled. Check if remote file exists.
--2012-02-16 16:23:06-- http://downloads.sourceforge.net/project/boost/boost-do
cs/1.47.0/boost_1_47_pdf222.zip?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Fboos
t%2F
Resolving downloads.sourceforge.net... 216.34.181.59
Connecting to downloads.sourceforge.net|216.34.181.59|:80... connected.
HTTP request sent, awaiting response... 404 Not Found
Remote file does not exist -- broken link!!!

7、增加重連次數用Wget -tries

在網路有問題的情況次選項尤其有用,預設是wget會重連20次以成功完成下載,我們可以把他增加為我們期待的次數

wget --tries=100 DOWNLOAD-URL


8、下載多個檔案/URLS用wget -i

首先把所有要下載的檔案或者URL存到一個記事本中,比如aa.txt,裡面內容如下

URL1
URL2
URL3
URL4

接下來輸入如下程式碼就可以批量下載了

wget -i aa.txt

9、下載一個完整的網站用wget -mirror

以下實現是你想完整的下載一個網站用於本地瀏覽

wget --mirror  -p --convert-links -P LOCAL-DIR WEBSITE-URL

--mirror:開啟映象選項

-p:下載所有用於顯示給定網址所必須的檔案

--convert-links:下載以後,轉換連結用於本地顯示

-P LOCAL_DIR:儲存所有的檔案或目錄到指定的目錄下

10、儲存輸出到日誌檔案而不是標準輸出用wget -o

當你想要把資訊儲存到一個檔案而不是在終端顯示時用以下程式碼。

wget -o download.log DOWNLOAD-URL

11、當超過指定大小時終止下載用wget -Q

當檔案已下載10M,此時你想停止下載可以使用下面的命令列

wget -Q10m -i FILE-WHICH-HAS-URLS

注意:此選項只能在下載多個檔案時有用,當你下載一個檔案時沒用。

12、下載特定檔案型別的檔案用wget -r -A

你可以用此方法下載一下檔案:

~從一個網站下載所有圖片
~從一個網站下載所有視訊

~從一個網站下載所有PDF檔案

wget -r -A.pdf http://url-to-webpage-with-pdfs/

13、指定不下載某一型別的檔案用wget --reject

你發現一個網站很有用,但是你不想下載上面的圖片,因為太佔流量,此時你可以用如下命令。

wget --reject=gif WEBSITE-TO-BE-DOWNLOADED


14、用wget實現FTP下載

匿名FTP下載用

wget ftp-url

有使用者名稱和密碼的FTP下載

wget --ftp-user=USERNAME --ftp-password=PASSWORD DOWNLOAD-URL

15、wget下載有的資源時必須用選項 --no-check-certificate,否則會提示沒有認證不允許下載

wget --no-check-certificate URL