プラスター業務日記 - プログラム(php,VC++などなど)やサーバ設定(Ubuntu Linux)などをメモがわりに書き残します。

wget でファイルをダウンロード

by なお (読完時間:推定 8分19秒|画像は1枚)

GNU wgetコンテンツのダウンロードを行う時にとても重宝する wget です。
サイトのミラーを取得したり、大きなisoファイルを取得したりといろいろ利用できます。
Cygwinに依存依存せずWindows環境にも導入できます。

wgetでWebサーバからファイルをダウンロード

一番基本的な使い方です。

$ wget http://サイト/file.tar.gz

例では圧縮ファイルになってますが基本的には公開されているコンテンツであればダウンロード可能です。

wgetで特定ファイルのみをダウンロード

png画像とgif画像のみ取得する場合などAオプションを指定します。
Aオプションは「-A 拡張子」で指定したファイルの拡張子のみダウンロードします。
複数の拡張子を指定する場合は拡張子をカンマで区切って指定します。

$ wget -A png,gif http://サイト/index.html

逆に取得不要なファイル拡張子に対してはRオプションを使用します。

-R txt,doc

wgetでサイトを一括ダンロード

起点サイトよりも上位に行かずに指定サイト内のコンテンツのみ一括で取得します。
Nオプションを使用することでローカルのファイルよりもサーバのファイルのタイムスタンプが新しい場合にダウンロードが行われます。

wget -r -p -np -t3 -c -N -l4 -L http://サイト/

あらかじめコンテンツ最大サイズがわかっている場合は問題ありませんが、サイズが不明な場合はQオプションを使用しダウンロード容量が指定容量に達したら中止させる安全策を講じた方が無難です。
以下の -Q10m と指定した場合は10Mダウンロードした時点で終了します。

wget -r -p -np -t3 -c -Q10m -N -l4 -L http://サイト/

wgetでHEADリクエスト

PHPのget_headers関数と同じです。
wgetはサーバレスポンスにLocationヘッダが含まれる場合自動でリダイレクトしてくれます。

$ wget --spider -S http://t.co/7KRPmuW5
Spider mode enabled. Check if remote file exists.
--2012-04-07 16:42:17--  http://t.co/7KRPmuW5
Resolving t.co (t.co)... 199.59.148.12
Connecting to t.co (t.co)|199.59.148.12|:80... connected.
HTTP request sent, awaiting response...
  HTTP/1.1 301 Moved Permanently
  Date: Mon, 09 Apr 2012 21:42:15 GMT
  Server: hi
  Location: http://j.plustar.jp/tcdtu
  Cache-Control: private,max-age=300
  Expires: Mon, 09 Apr 2012 21:47:15 GMT
  Connection: close
  Content-Type: text/html; charset=UTF-8
Location: http://j.plustar.jp/tcdtu [following]
Spider mode enabled. Check if remote file exists.
--2012-04-07 16:42:17--  http://j.plustar.jp/tcdtu
Resolving j.plustar.jp (j.plustar.jp)... 203.135.130.10
Connecting to j.plustar.jp (j.plustar.jp)|203.135.130.10|:80... connected.
HTTP request sent, awaiting response...
  HTTP/1.0 301 Moved Permanently
  Date: Mon, 09 Apr 2012 21:42:15 GMT
  Server: Apache
  X-Powered-By: PHP/5.3.5-1ubuntu7.4
  Location: http://wired.jp/2012/03/20/mark-shuttleworth-ubuntu/
  Vary: Accept-Encoding
  Connection: close
  Content-Type: text/html
Location: http://wired.jp/2012/03/20/mark-shuttleworth-ubuntu/ [following]
Spider mode enabled. Check if remote file exists.
--2012-04-07 16:42:17--  http://wired.jp/2012/03/20/mark-shuttleworth-ubuntu/
Resolving wired.jp (wired.jp)... 125.6.160.173
Connecting to wired.jp (wired.jp)|125.6.160.173|:80... connected.
HTTP request sent, awaiting response...
  HTTP/1.1 200 OK
  Date: Mon, 09 Apr 2012 21:42:15 GMT
  Server: Apache/2.2.17
  Last-Modified: Mon, 09 Apr 2012 21:19:04 GMT
  ETag: "1be8ce4-b1cc-4bd458e432200"
  Accept-Ranges: bytes
  Content-Length: 45516
  X-Pingback: http://wired.jp/xmlrpc.php
  X-Powered-By: W3 Total Cache/0.9.1.3
  Vary: Accept-Encoding,Cookie
  Connection: close
  Content-Type: text/html; charset=UTF-8
Length: 45516 (44K) 1
Remote file exists and could contain further links,
but recursion is disabled -- not retrieving.

wgetでリンク切れチェック

wgetはリンク切れチェックツールではないので、どのページでリンク切れがあるかまでは分かりませんがリリース前にリンク切れ一括チェックを行うには高速で便利です。

$ wget --spider --no-directories --recursive --no-verbose http://サイト/

最後にレポートがされます。

Found 2 broken links.
http://www.plustar.jp/trapper/report/favicon.icohttp://www.plustar.jp/corporate/press/favicon.icoFINISHED --2012-04-07 10:13:38--
Downloaded: 349 files, 4.5M in 0s (31791 GB/s)

wgetをProxy経由で使う

/etc/wgetrcを修正します。
修正開所は以下の部分です。

# You can set the default proxies for Wget to use for http, https, and ftp.
# They will override the value in the environment.
#https_proxy = http://proxy.yoyodyne.com:18023/
http_proxy = http://proxy.yoyodyne.com:18023/
ftp_proxy = http://proxy.yoyodyne.com:18023/

# If you do not want to use proxy at all, set this to off.
use_proxy = on

Windows環境でwgetを動かす

Windows環境でもCygwinに依存する事なくwget単体で動かすことができます。
Wget for Windows
Windowsでwgetを動かすには以下のDLLが必要です。

  • libeay32.dll
  • libiconv2.dll
  • libintl3.dll
  • libssl32.dll

別途取得してwgetと同じディレクトリには入れて下さい。

【改訂新版】 Linuxコマンド ポケットリファレンス (Pocket Reference)

【改訂新版】 Linuxコマンド ポケットリファレンス (Pocket Reference)

著者/訳者:沓名 亮典 平山 智恵

出版社:技術評論社( 2009-04-08 )

定価:¥ 2,462

単行本(ソフトカバー) ( 576 ページ )

ISBN-10 : 4774138169

ISBN-13 : 9784774138169


カテゴリ:メモ  | コメントは受け付けていません。

Comments are closed.