Your script is almost correct. NASA page structure:
<html> <body> ... <a href="http://.../blah.jpg"><img src="http://.../blah-lowres.jpg"></a> ... </body> </html>
So, if $mech->follow_link successful, you already have the image data in $mech->content .
Try the following:
$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";
Erikr source share