Web::Scraperで画像URL取得して保存してみた
WWW::Slmameに、ソラマメブログに貼付けてる画像を一式ダウンロードする関数を付けてみようとおもいたって、とりあえずWeb::Scraperでスクレイピングすることに。
が、自身の管理してるブログには画像が一個も上がってない事実にきがつき、とりあえずそこそこの画像数のブログでやってみることにした。とりあえずゼロスタで試してみる。
ソラマメブログには、albumページという、今までにアップした画像を一覧で表示してくれる代物があるため、そこを利用。
$ scraper "http://zerostyle.slmame.com/album.html"
scraper> process 'div.album_image a img', 'src[]' => '@src';
scraper> y
- -
src:
- !!perl/scalar:URI::http http://img01.slmame.com/usr/zerostyle/Maya_MH-s.jpg
- !!perl/scalar:URI::http http://img01.slmame.com/usr/zerostyle/Justine_MH-s.jpg
- !!perl/scalar:URI::http http://img01.slmame.com/usr/zerostyle/Anne_MH-s.jpg
- !!perl/scalar:URI::http http://img01.slmame.com/usr/zerostyle/kaijou_Setumei-s.jpg
- !!perl/scalar:URI::http http://img01.slmame.com/usr/zerostyle/Quilla_MH-s.jpg
.
.
とおもったけど、これだとurlがハッシュで取れない気がしてきた…。そこでちょっとやり方を変えてみる。
scraper> process 'div.album_image a', 'link[]' => scraper { process 'img', 'img' => '@src'; }
scraper> y
- -
link:
- img: !!perl/scalar:URI::http http://img01.slmame.com/usr/zerostyle/Maya_MH-s.jpg
- img: !!perl/scalar:URI::http http://img01.slmame.com/usr/zerostyle/Justine_MH-s.jpg
- img: !!perl/scalar:URI::http http://img01.slmame.com/usr/zerostyle/Anne_MH-s.jpg
- img: !!perl/scalar:URI::http http://img01.slmame.com/usr/zerostyle/kaijou_Setumei-s.jpg
- img: !!perl/scalar:URI::http http://img01.slmame.com/usr/zerostyle/Quilla_MH-s.jpg
- img: !!perl/scalar:URI::http http://img01.slmame.com/usr/zerostyle/Emi_UP-s.jpg
- img: !!perl/scalar:URI::http http://img01.slmame.com/usr/zerostyle/Aco_MH-s.jpg
- img: !!perl/scalar:URI::http http://img01.slmame.com/usr/zerostyle/Ray_UP-s.jpg
- img: !!perl/scalar:URI::http http://img01.slmame.com/usr/zerostyle/asuna-s.jpg
良好。
$self->{wm}->get("http://zerostyle.slmame.com/album.html"); my $scraper = scraper{ process 'div.album_image a', 'link[]' => scraper { process 'img', 'img' => '@src'; } }->scrape($self->{wm}->content); foreach my $url (@{$scraper->{link}}){ $self->{wm}->get($url->{img}); $url->{img} =~ m/^http:\/\/[^.]+\.slmame\.com\/usr\/[^\/]+\/(.+)$/; my $saveimage = $1; open(my $out,">","./$saveimage") || die; binmode $out; print $out $self->{wm}->content; close($out); } }
結果。
んで、オチとしては、このAlbumページは画像一覧ではなくて、画像が含まれるエントリーの一覧だったとかなんとか…。もうちょい弄ることにします。