XVirtusX
XVirtusX

Reputation: 709

Trouble following an image link with WWW::Mechanize

I'm working on a Perl script to get the "astronomy image of the day" and set it as my wallpaper. Then I would set a cronjob to do that everyday for me. But I'm having a hard time making the script follow the image link that leads to the full-sized image, and only then, download it. I was trying something like that code bellow(have in mind that I'm only a Perl beginner who does not know much about Perl regex):

#!/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);

Any help would be much appreciated!

Upvotes: 4

Views: 482

Answers (1)

ErikR
ErikR

Reputation: 52039

Your script is almost correct. The structure of the NASA page is:

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

So, if $mech->follow_link succeeds you already have the image data in $mech->content.

Try this:

$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";

Upvotes: 3

Related Questions