HTMLからリンクを抽出するスクリプト

softdown-icon-linux-tux.png (Linux)HTMLからリンク・画像urlを抽出するスクリプト
Extract link/image url from html (Perl Script)



戻る一つ前のメニューに戻る

目次

ソフトウエアのダウンロード

soft-ico-download.gif PerlソースコードをSubversionブラウザよりダウンロード


機能の概要

指定したWebページ内のリンク(a href="...")や画像(img src="...")のurlを、指定した条件のもとに抽出するスクリプト。

データや画像を連続(一括)ダウンロードするときの手助けとなりますが、webサーバに対して短い時間に多数のアクセスを行うこともできますので、利用方法には注意が必要です。

使用法
extract-html-alink.pl ([a|img]) [url_list_file.txt|url] ([domain|+|-] [path])
スクリプト引数の説明
  • a : a link タグより抽出(指定しない場合のデフォルト値)
  • img : img タグより抽出
  • url_list_file.txt : urlを列挙したテキストファイル
  • url : urlを直接指定(例 http://example.com/path/target.html
  • domain : 指定したドメイン名に一致するもののみ
+ の場合はurlのドメイン名と一致
- の場合は全てに一致
  • path : パス名に一致するもののみ


動作イメージ

NASAが公開している天文画像を一括ダウンロードする例。

terminalで実行
$ extract-html-alink.pl a http://hubblesite.org/gallery/album/nebula > step-1.txt

得られたstep-1.txtの不必要な行を削除し、取得したいページのみ残す

step-1.txt 編集後
http://hubblesite.org/gallery/album/nebula/pr2006001a/
http://hubblesite.org/gallery/album/nebula/pr2004032d/
http://hubblesite.org/gallery/album/nebula/pr1995044a/
http://hubblesite.org/gallery/album/nebula/pr2013012a/
terminalで実行
$ extract-html-alink.pl a step-1.txt - large > step-2.txt

得られたstep-2.txtの不必要な行を削除し、取得したいページのみ残す

step-2.txt 編集後
http://hubblesite.org/newscenter/archive/releases/2006/01/image/a/format/large_web/
http://hubblesite.org/newscenter/archive/releases/2006/01/image/a/format/xlarge_web/
http://hubblesite.org/gallery/album/nebula/pr2004032d/large_web/
http://hubblesite.org/gallery/album/nebula/pr1995044a/large_web/
http://hubblesite.org/gallery/album/nebula/pr2013012a/large_web/
http://hubblesite.org/gallery/album/nebula/pr2013012a/xlarge_web/
terminalで実行
$ extract-html-alink.pl img step-2.txt imgsrc.hubblesite.org large > step-3.txt
最終的に得られた step-3.txt
http://imgsrc.hubblesite.org/hu/db/images/hs-2006-01-a-large_web.jpg
http://imgsrc.hubblesite.org/hu/db/images/hs-2006-01-a-xlarge_web.jpg
http://imgsrc.hubblesite.org/hu/db/images/hs-2004-32-d-large_web.jpg
http://imgsrc.hubblesite.org/hu/db/images/hs-1995-44-a-large_web.jpg
http://imgsrc.hubblesite.org/hu/db/images/hs-2013-12-a-large_web.jpg
http://imgsrc.hubblesite.org/hu/db/images/hs-2013-12-a-xlarge_web.jpg

出力されたファイルをwgetコマンドで一括ダウンロードする

terminalで実行
$ cat step-3.txt | xargs wget


動作確認済み

  • Ubuntu 12.04


バージョンアップ情報

  • Version 0.1 (2011/02/13)
    当初
  • Version 0.2 (2014/01/03)
    url/urlリストファイル, img/a対象, フィルタ


使用許諾条件

GNU GPL フリーソフトウエア

GNU 一般公衆利用許諾書 (GNU General Public License v3)

戻る一つ前のメニューに戻る