![]() |
(Linux)HTMLからリンク・画像urlを抽出するスクリプト Extract link/image url from html (Perl Script) |
目次 |
指定したWebページ内のリンク(a href="...")や画像(img src="...")のurlを、指定した条件のもとに抽出するスクリプト。
データや画像を連続(一括)ダウンロードするときの手助けとなりますが、webサーバに対して短い時間に多数のアクセスを行うこともできますので、利用方法には注意が必要です。
extract-html-alink.pl ([a|img]) [url_list_file.txt|url] ([domain|+|-] [path])
NASAが公開している天文画像を一括ダウンロードする例。
$ extract-html-alink.pl a http://hubblesite.org/gallery/album/nebula > 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/
$ extract-html-alink.pl a step-1.txt - large > 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/
$ extract-html-alink.pl img step-2.txt imgsrc.hubblesite.org large > 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
コマンドで一括ダウンロードする
$ cat step-3.txt | xargs wget
GNU GPL フリーソフトウエア