Skip to content

Instantly share code, notes, and snippets.

@satococoa
Created January 16, 2009 12:21
Show Gist options
  • Save satococoa/47909 to your computer and use it in GitHub Desktop.
Save satococoa/47909 to your computer and use it in GitHub Desktop.
#!/opt/local/bin/perl
# Webページからリンクの張られた画像をカレントディレクトリにダウンロード(perl+regexp版)
use strict;
use warnings;
require LWP::UserAgent;
my @urls = @ARGV;
my $ua = LWP::UserAgent->new;
$ua->timeout(10);
$ua->show_progress(1);
foreach my $url (@urls) {
my $response = $ua->get($url);
if ($response->is_success) {
my $doc = $response->content;
my @imgurls = grep {/<a.+href="([^"]+?\.(jpg|gif|png))"/} split(/\n/, $doc);
@imgurls = map {s/.*<a.+href="([^"]+?\.(jpg|gif|png))".*/$1/g;$_;} @imgurls;
foreach my $imgurl (@imgurls) {
my $res = $ua->get($imgurl);
if ($res->is_success) {
$imgurl =~ s%.+/(.+)$%$1%;
my $filename = $imgurl;
open my $fh, '>', $filename or die $!;
print $fh $res->content;
}
}
} else {
print STDERR "Error: " . $response->status_line . "\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment