下载特定图像

时间:2010-05-31 02:59:46

标签: perl

我正在尝试搜索和下载特定图像/网站的封面/封底(如果找到),但无论我做什么,我总是只下载其中一个。如果找到,我应该如何更改我的代码以下载它们?

use strict;
use warnings;
use LWP;
use LWP::UserAgent;
use URI::Escape;
use HTTP::Status;

getCover(...);

sub getCover {

......


  while ($title_found =~ /'(http:\/\/images.blu-ray.com\/movies\/covers\/\d+_.*?)'/gis) {          
     $url = getSite($1);
     if ($title_found =~ /front/) {
        $filename = 'front.jpg';        
     }
     elsif ($title_found =~ /back/) {
        $filename = 'back.jpg';
     }
   }
   my $dir = 'somepath'.$filename;     
   open F, ">", $dir;          
        binmode F;
        print F $url;
   close F; 
   return 0;
}


sub getSite {
 $url = shift;

 print "URL: $url\n";

 my $r;
 my $ua = new LWP::UserAgent();

 $ua->agent("Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.3) Gecko/20030312");

 my $req = new HTTP::Request GET => $url;

 $req->push_header("Accept-Language", "en");

 $req = $ua->prepare_request($req);

 my $res = $ua->request($req);
 my $rc = $res->code;

 if(is_success($rc)){
     $r = $res->as_string();
     $r = $res->content();
     }

     else {
     print "Failed\n";
     }

 return $r;
}    

1 个答案:

答案 0 :(得分:2)

尝试将保存到'somepath'.$filename的部分放在while循环中而不是在其外部。

此外,似乎$title_found应包含多个网址。在这种情况下,您需要将$1保存到临时变量,并在其中查找前/后而不是$title_found。否则,您最终会将两个封面保存到front.jpg