与WWW的图像链接后的麻烦::机械化

问题描述:

我正在研究Perl脚本以获得“天文学图像”并将其设置为我的壁纸。然后我会设置一个cronjob为我每天做这件事。但是我很难让脚本沿着图像链接进入全尺寸图像,只有这样,才能下载它。 我是想这样的事情代码波纹管(有记住,我只是一个Perl的初学者谁不知道很多关于Perl的正则表达式):与WWW的图像链接后的麻烦::机械化

#!/usr/bin/perl -w 
use strict; 
use warnings; 
use WWW::Mechanize; 

my $url = "http://apod.nasa.gov/apod/astropix.html"; 

my $mech = WWW::Mechanize->new(); 
$mech->get($url); 
    #debugging 
if ($mech->follow_link(url_regex=>qr/\.(?:jpg|png)$/)){ 
    print "Following the image link..."; 
}else{ 
    print "Couldn't find the link..."; 
} 

my @img = $mech->find_image(alt_regex => qr/image/i); 

    foreach my $img(@img){ 
    $mech->get($img->url, ':content_file'=>'astro.jpg'); 
    } 

    print "\n"; 

    exit(0); 

任何帮助,将不胜感激!

+0

参见http://perlmonks.org/?node_id=978153 –

你的脚本几乎是正确的。美国航空航天局网页的结构是:

<html> 
<body> 
    ... 
    <a href="http://.../blah.jpg"><img src="http://.../blah-lowres.jpg"></a> 
    ... 
</body> 
</html> 

所以,如果$mech->follow_link成功,你已经在$mech->content的图像数据。

试试这个:

$mech->get($url) or die "unable to get $url"; 
$mech->follow_link(url_regex => qr/\.(jpg|png)\z/) or die "unable to follow image link"; 
open(my $fh, ">astro.jpg"); 
print {$fh} $mech->content; 
close($fh); 
print "saved image as astro.jpg\n"; 
+0

非常感谢您!它像一个魅力! – XVirtusX