我在工作中, 經常下載遙感影像,每個影像都很大,使用普通的c#編寫的ftphelpe下載不太穩定(網絡太差了),最終選擇了linux下一款牛逼的下載工具wget,使用它的windows移植版本的。在此寫此文,希望對和我一樣保證數據下載完整的朋友們提供一個思路。
參考文檔
http://man.linuxde.net/wget
http://gnuwin32.sourceforge.net/packages/wget.htm
https://wiki.archlinux.org/index.php/Wget
https://eternallybored.org/misc/wget/
https://eternallybored.org/misc/wget/manual/wget-1.16.2.html
什么是wget
wget是一個從網絡上自動下載文件的自由工具,支持通過HTTP、HTTPS、FTP三個最常見的TCP/IP下載,並可以使用HTTP代理。wget名稱的由來是“World Wide Web”與“get”的結合。
wget的獲取
linux系統 | 默認就安裝有wget工具, 如果沒有,使用yum install wget即可(前提配置正確的yum源,不會的可以參照我的另外一篇博客http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_linux_002.html) |
windows系統 | 可以百度wget for windows 獲取, 這里提供一個地址:http://gnuwin32.sourceforge.net/packages/wget.htm,進入頁面,找到那個download下的Binaries后的bin下載即可。這里提供一個1.11.4的下載地址:http://downloads.sourceforge.net/gnuwin32/wget-1.11.4-1-bin.zip |
注意有時候需要請求帶有認證的https地址的時候,可能需要wget的版本高於1.8的。從這個地址找對應的版本: https://eternallybored.org/misc/wget/
wget的好處
- 可以使用REST和RANGE恢復已中止的下載
- 可以使用文件名通配符和遞歸鏡像目錄
- 許多不同語言的基於NLS的消息文件
- 可選地,將下載的文檔中的絕對鏈接轉換為相對鏈接,以便下載的文檔可以在本地鏈接到彼此
- 在大多數類UNIX操作系統以及Microsoft Windows上運行
- 支持HTTP代理
- 支持HTTP cookie
- 支持持久HTTP連接
- 無人值守/后台操作
- 使用本地文件時間戳來確定是否需要在鏡像時重新下載文檔
- GNU Wget是根據GNU通用公共許可證分發的。
wget怎么用:
用法: wget [選項]... [URL]...
wget常用選項
- -a<日志文件>:在指定的日志文件中記錄資料的執行過程;
- -A<后綴名>:指定要下載文件的后綴名,多個后綴名之間使用逗號進行分隔;
- -b:進行后台的方式運行wget;
- -B<連接地址>:設置參考的連接地址的基地地址;
- -c:繼續執行上次終端的任務;
- -C<標志>:設置服務器數據塊功能標志on為激活,off為關閉,默認值為on;
- -d:調試模式運行指令;
- -D<域名列表>:設置順着的域名列表,域名之間用“,”分隔;
- -e<指令>:作為文件“.wgetrc”中的一部分執行指定的指令;
- -h:顯示指令幫助信息;
- -i<文件>:從指定文件獲取要下載的URL地址;
- -l<目錄列表>:設置順着的目錄列表,多個目錄用“,”分隔;
- -L:僅順着關聯的連接;
- -r:遞歸下載方式;
- -nc:文件存在時,下載文件不覆蓋原有文件;
- -nv:下載時只顯示更新和出錯信息,不顯示指令的詳細執行過程;
- -q:不顯示指令執行過程;
- -nh:不查詢主機名稱;
- -v:顯示詳細執行過程;
- -V:顯示版本信息;
- --passive-ftp:使用被動模式PASV連接FTP服務器;
- --follow-ftp:從HTML文件中下載FTP連接文件。
來自: http://man.linuxde.net/wget
wget所有選項
windows 環境:可以使用 C:\Users\Administrator\Downloads\wget-1.11.4-1-bin\bin>wget --help >> wgetHelp.txt 這個命令 wget的幫助信息重定向到wgetHelp.txt中去詳細查看。
linux環境:可以使用man wget 查看幫助信息。
GNU Wget 1.11.4,非交互式的網絡文件下載工具。 用法: wget [選項]... [URL]... Mandatory arguments to long options are mandatory for short options too. Startup: -V, --version display the version of Wget and exit. -h, --help print this help. -b, --background go to background after startup. -e, --execute=COMMAND execute a `.wgetrc'-style command. Logging and input file: -o, --output-file=FILE log messages to FILE. -a, --append-output=FILE append messages to FILE. -d, --debug print lots of debugging information. -q, --quiet quiet (no output). -v, --verbose be verbose (this is the default). -nv, --no-verbose turn off verboseness, without being quiet. -i, --input-file=FILE download URLs found in FILE. -F, --force-html treat input file as HTML. -B, --base=URL prepends URL to relative links in -F -i file. Download: -t, --tries=NUMBER set number of retries to NUMBER (0 unlimits). --retry-connrefused retry even if connection is refused. -O, --output-document=FILE write documents to FILE. -nc, --no-clobber skip downloads that would download to existing files. -c, --continue resume getting a partially-downloaded file. --progress=TYPE select progress gauge type. -N, --timestamping don't re-retrieve files unless newer than local. -S, --server-response print server response. --spider don't download anything. -T, --timeout=SECONDS set all timeout values to SECONDS. --dns-timeout=SECS set the DNS lookup timeout to SECS. --connect-timeout=SECS set the connect timeout to SECS. --read-timeout=SECS set the read timeout to SECS. -w, --wait=SECONDS wait SECONDS between retrievals. --waitretry=SECONDS wait 1..SECONDS between retries of a retrieval. --random-wait wait from 0...2*WAIT secs between retrievals. --no-proxy explicitly turn off proxy. -Q, --quota=NUMBER set retrieval quota to NUMBER. --bind-address=ADDRESS bind to ADDRESS (hostname or IP) on local host. --limit-rate=RATE limit download rate to RATE. --no-dns-cache disable caching DNS lookups. --restrict-file-names=OS restrict chars in file names to ones OS allows. --ignore-case ignore case when matching files/directories. -4, --inet4-only connect only to IPv4 addresses. -6, --inet6-only connect only to IPv6 addresses. --prefer-family=FAMILY connect first to addresses of specified family, one of IPv6, IPv4, or none. --user=USER set both ftp and http user to USER. --password=PASS set both ftp and http password to PASS. Directories: -nd, --no-directories don't create directories. -x, --force-directories force creation of directories. -nH, --no-host-directories don't create host directories. --protocol-directories use protocol name in directories. -P, --directory-prefix=PREFIX save files to PREFIX/... --cut-dirs=NUMBER ignore NUMBER remote directory components. HTTP options: --http-user=USER set http user to USER. --http-password=PASS set http password to PASS. --no-cache disallow server-cached data. -E, --html-extension save HTML documents with `.html' extension. --ignore-length ignore `Content-Length' header field. --header=STRING insert STRING among the headers. --max-redirect maximum redirections allowed per page. --proxy-user=USER set USER as proxy username. --proxy-password=PASS set PASS as proxy password. --referer=URL include `Referer: URL' header in HTTP request. --save-headers save the HTTP headers to file. -U, --user-agent=AGENT identify as AGENT instead of Wget/VERSION. --no-http-keep-alive disable HTTP keep-alive (persistent connections). --no-cookies don't use cookies. --load-cookies=FILE load cookies from FILE before session. --save-cookies=FILE save cookies to FILE after session. --keep-session-cookies load and save session (non-permanent) cookies. --post-data=STRING use the POST method; send STRING as the data. --post-file=FILE use the POST method; send contents of FILE. --content-disposition honor the Content-Disposition header when choosing local file names (EXPERIMENTAL). --auth-no-challenge Send Basic HTTP authentication information without first waiting for the server's challenge. HTTPS (SSL/TLS) options: --secure-protocol=PR choose secure protocol, one of auto, SSLv2, SSLv3, and TLSv1. --no-check-certificate don't validate the server's certificate. --certificate=FILE client certificate file. --certificate-type=TYPE client certificate type, PEM or DER. --private-key=FILE private key file. --private-key-type=TYPE private key type, PEM or DER. --ca-certificate=FILE file with the bundle of CA's. --ca-directory=DIR directory where hash list of CA's is stored. --random-file=FILE file with random data for seeding the SSL PRNG. --egd-file=FILE file naming the EGD socket with random data. FTP options: --ftp-user=USER set ftp user to USER. --ftp-password=PASS set ftp password to PASS. --no-remove-listing don't remove `.listing' files. --no-glob turn off FTP file name globbing. --no-passive-ftp disable the "passive" transfer mode. --retr-symlinks when recursing, get linked-to files (not dir). --preserve-permissions preserve remote file permissions. Recursive download: -r, --recursive specify recursive download. -l, --level=NUMBER maximum recursion depth (inf or 0 for infinite). --delete-after delete files locally after downloading them. -k, --convert-links make links in downloaded HTML point to local files. -K, --backup-converted before converting file X, back up as X.orig. -m, --mirror shortcut for -N -r -l inf --no-remove-listing. -p, --page-requisites get all images, etc. needed to display HTML page. --strict-comments turn on strict (SGML) handling of HTML comments. Recursive accept/reject: -A, --accept=LIST comma-separated list of accepted extensions. -R, --reject=LIST comma-separated list of rejected extensions. -D, --domains=LIST comma-separated list of accepted domains. --exclude-domains=LIST comma-separated list of rejected domains. --follow-ftp follow FTP links from HTML documents. --follow-tags=LIST comma-separated list of followed HTML tags. --ignore-tags=LIST comma-separated list of ignored HTML tags. -H, --span-hosts go to foreign hosts when recursive. -L, --relative follow relative links only. -I, --include-directories=LIST list of allowed directories. -X, --exclude-directories=LIST list of excluded directories. -np, --no-parent don't ascend to the parent directory. 請將錯誤報告或建議寄給 <bug-wget@gnu.org>。
翻譯后就是下面的啦:
啟動: -V, --version 顯示 Wget 的版本信息並退出。 -h, --help 打印此幫助。 -b, --background 啟動后轉入后台。 -e, --execute=COMMAND 運行一個“.wgetrc”風格的命令。 日志和輸入文件: -o, --output-file=FILE 將日志信息寫入 FILE。 -a, --append-output=FILE 將信息添加至 FILE。 -d, --debug 打印大量調試信息。 -q, --quiet 安靜模式 (無信息輸出)。 -v, --verbose 詳盡的輸出 (此為默認值)。 -nv, --no-verbose 關閉詳盡輸出,但不進入安靜模式。 -i, --input-file=FILE 下載本地或外部 FILE 中的 URLs。 -F, --force-html 把輸入文件當成 HTML 文件。 -B, --base=URL 解析與 URL 相關的 HTML 輸入文件 (由 -i -F 選項指定)。 --config=FILE Specify config file to use. 下載: -t, --tries=NUMBER 設置重試次數為 NUMBER (0 代表無限制)。 --retry-connrefused 即使拒絕連接也是重試。 -O, --output-document=FILE 將文檔寫入 FILE。 -nc, --no-clobber skip downloads that would download to existing files (overwriting them). -c, --continue 斷點續傳下載文件。 --progress=TYPE 選擇進度條類型。 -N, --timestamping 只獲取比本地文件新的文件。 --no-use-server-timestamps 不用服務器上的時間戳來設置本地文件。 -S, --server-response 打印服務器響應。 --spider 不下載任何文件。 -T, --timeout=SECONDS 將所有超時設為 SECONDS 秒。 --dns-timeout=SECS 設置 DNS 查尋超時為 SECS 秒。 --connect-timeout=SECS 設置連接超時為 SECS 秒。 --read-timeout=SECS 設置讀取超時為 SECS 秒。 -w, --wait=SECONDS 等待間隔為 SECONDS 秒。 --waitretry=SECONDS 在獲取文件的重試期間等待 1..SECONDS 秒。 --random-wait 獲取多個文件時,每次隨機等待間隔 0.5*WAIT...1.5*WAIT 秒。 --no-proxy 禁止使用代理。 -Q, --quota=NUMBER 設置獲取配額為 NUMBER 字節。 --bind-address=ADDRESS 綁定至本地主機上的 ADDRESS (主機名或是 IP)。 --limit-rate=RATE 限制下載速率為 RATE。 --no-dns-cache 關閉 DNS 查尋緩存。 --restrict-file-names=OS 限定文件名中的字符為 OS 允許的字符。 --ignore-case 匹配文件/目錄時忽略大小寫。 -4, --inet4-only 僅連接至 IPv4 地址。 -6, --inet6-only 僅連接至 IPv6 地址。 --prefer-family=FAMILY 首先連接至指定協議的地址 FAMILY 為 IPv6,IPv4 或是 none。 --user=USER 將 ftp 和 http 的用戶名均設置為 USER。 --password=PASS 將 ftp 和 http 的密碼均設置為 PASS。 --ask-password 提示輸入密碼。 --no-iri 關閉 IRI 支持。 --local-encoding=ENC IRI (國際化資源標識符) 使用 ENC 作為本地編碼。 --remote-encoding=ENC 使用 ENC 作為默認遠程編碼。 --unlink remove file before clobber. 目錄: -nd, --no-directories 不創建目錄。 -x, --force-directories 強制創建目錄。 -nH, --no-host-directories 不要創建主目錄。 --protocol-directories 在目錄中使用協議名稱。 -P, --directory-prefix=PREFIX 以 PREFIX/... 保存文件 --cut-dirs=NUMBER 忽略遠程目錄中 NUMBER 個目錄層。 HTTP 選項: --http-user=USER 設置 http 用戶名為 USER。 --http-password=PASS 設置 http 密碼為 PASS。 --no-cache 不在服務器上緩存數據。 --default-page=NAME 改變默認頁 (默認頁通常是“index.html”)。 -E, --adjust-extension 以合適的擴展名保存 HTML/CSS 文檔。 --ignore-length 忽略頭部的‘Content-Length’區域。 --header=STRING 在頭部插入 STRING。 --max-redirect 每頁所允許的最大重定向。 --proxy-user=USER 使用 USER 作為代理用戶名。 --proxy-password=PASS 使用 PASS 作為代理密碼。 --referer=URL 在 HTTP 請求頭包含‘Referer: URL’。 --save-headers 將 HTTP 頭保存至文件。 -U, --user-agent=AGENT 標識為 AGENT 而不是 Wget/VERSION。 --no-http-keep-alive 禁用 HTTP keep-alive (永久連接)。 --no-cookies 不使用 cookies。 --load-cookies=FILE 會話開始前從 FILE 中載入 cookies。 --save-cookies=FILE 會話結束后保存 cookies 至 FILE。 --keep-session-cookies 載入並保存會話 (非永久) cookies。 --post-data=STRING 使用 POST 方式;把 STRING 作為數據發送。 --post-file=FILE 使用 POST 方式;發送 FILE 內容。 --content-disposition 當選中本地文件名時 允許 Content-Disposition 頭部 (尚在實驗)。 --auth-no-challenge 發送不含服務器詢問的首次等待 的基本 HTTP 驗證信息。 HTTPS (SSL/TLS) 選項: --secure-protocol=PR 選擇安全協議,可以是 auto、SSLv2、 SSLv3 或是 TLSv1 中的一個。 --no-check-certificate 不要驗證服務器的證書。 --certificate=FILE 客戶端證書文件。 --certificate-type=TYPE 客戶端證書類型,PEM 或 DER。 --private-key=FILE 私鑰文件。 --private-key-type=TYPE 私鑰文件類型,PEM 或 DER。 --ca-certificate=FILE 帶有一組 CA 認證的文件。 --ca-directory=DIR 保存 CA 認證的哈希列表的目錄。 --random-file=FILE 帶有生成 SSL PRNG 的隨機數據的文件。 --egd-file=FILE 用於命名帶有隨機數據的 EGD 套接字的文件。 FTP 選項: --ftp-user=USER 設置 ftp 用戶名為 USER。 --ftp-password=PASS 設置 ftp 密碼為 PASS。 --no-remove-listing 不要刪除‘.listing’文件。 --no-glob 不在 FTP 文件名中使用通配符展開。 --no-passive-ftp 禁用“passive”傳輸模式。 --retr-symlinks 遞歸目錄時,獲取鏈接的文件 (而非目錄)。 遞歸下載: -r, --recursive 指定遞歸下載。 -l, --level=NUMBER 最大遞歸深度 (inf 或 0 代表無限制,即全部下載)。 --delete-after 下載完成后刪除本地文件。 -k, --convert-links 讓下載得到的 HTML 或 CSS 中的鏈接指向本地文件。 -K, --backup-converted 在轉換文件 X 前先將它備份為 X.orig。 -m, --mirror -N -r -l inf --no-remove-listing 的縮寫形式。 -p, --page-requisites 下載所有用於顯示 HTML 頁面的圖片之類的元素。 --strict-comments 用嚴格方式 (SGML) 處理 HTML 注釋。 遞歸接受/拒絕: -A, --accept=LIST 逗號分隔的可接受的擴展名列表。 -R, --reject=LIST 逗號分隔的要拒絕的擴展名列表。 -D, --domains=LIST 逗號分隔的可接受的域列表。 --exclude-domains=LIST 逗號分隔的要拒絕的域列表。 --follow-ftp 跟蹤 HTML 文檔中的 FTP 鏈接。 --follow-tags=LIST 逗號分隔的跟蹤的 HTML 標識列表。 --ignore-tags=LIST 逗號分隔的忽略的 HTML 標識列表。 -H, --span-hosts 遞歸時轉向外部主機。 -L, --relative 只跟蹤有關系的鏈接。 -I, --include-directories=LIST 允許目錄的列表。 --trust-server-names use the name specified by the redirection url last component. -X, --exclude-directories=LIST 排除目錄的列表。 -np, --no-parent 不追溯至父目錄。
wget常用用法
1.使用wget下載單個文件
wget http://dldir1.qq.com/qqfile/qq/QQ8.7/19113/QQ8.7.exe
2.下載並以不同的文件名保存
wget -O qq.exe http://dldir1.qq.com/qqfile/qq/QQ8.7/19113/QQ8.7.exe
3.wget限速下載
wget --limit-rate=300k http://dldir1.qq.com/qqfile/qq/QQ8.7/19113/QQ8.7.exe
4.使用wget后台下載
wget -b http://dldir1.qq.com/qqfile/qq/QQ8.7/19113/QQ8.7.exe
5.使用wget斷點續傳
wget -c http://dldir1.qq.com/qqfile/qq/QQ8.7/19113/QQ8.7.exe
6.偽裝代理名稱下載
wget --user-agent="Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.204 Safari/534.16" http://dldir1.qq.com/qqfile/qq/QQ8.7/19113/QQ8.7.exe
7.測試下載鏈接
wget --spider http://dldir1.qq.com/qqfile/qq/QQ8.7/19113/QQ8.7.exe
8.增加重試次數
wget --tries=40 http://dldir1.qq.com/qqfile/qq/QQ8.7/19113/QQ8.7.exe
9.下載多個文件
wget -i filelist.txt
其中filelist 里面都是url, 通常以行分割
10.鏡像網站
wget --mirror -p --convert-links -P ./LOCAL URL
--miror開戶鏡像下載。
-p下載所有為了html頁面顯示正常的文件。
--convert-links下載后,轉換成本地的鏈接。
-P ./LOCAL保存所有文件和目錄到本地指定目錄
11.過濾指定格式下載
wget --reject=gif url
12.把下載信息存入日志文件
wget -o download.log http://dldir1.qq.com/qqfile/qq/QQ8.7/19113/QQ8.7.exe
13.下載指定格式文件
wget -r -A.pdf url
15.FTP下載
wget --ftp-user=USERNAME --ftp-password=PASSWORD url
其他