This is my last question for this I hope. I am using $mech->follow_link to try to download a file. For some reason though the file saved is just the page I first pull up and not the link I want to follow. Is this the correct way I should download the file from the link? I do not want to use wget.
#!/usr/bin/perl -w
use strict;
use LWP;
use WWW::Mechanize;
my $now_string = localtime;
my $mech = WWW::Mechanize->new();
my $filename = join(' ', split(/\W++/, $now_string, -1));
$mech->credentials( '***********' , '************'); # if you do need to supply server and realms use credentials like in [LWP doc][2]
$mech->get('http://datawww2.wxc.com/kml/echo/MESH_Max_180min/') or die "Error: failed to load the web page";
$mech->follow_link( url_regex => qr/MESH/i ) or die "Error: failed to download content";
$mech->save_content("$filename.kmz");
get
, to make sure you're reaching a valid HTML pageget
#!/usr/bin/perl -w
use strict;
use WWW::Mechanize;
sub main{
my $url = qq(http://www.kmzlinks.com);
my $dest = qq($ENV{HOME}/Desktop/destfile.kmz);
my $mech = WWW::Mechanize->new(autocheck => 1);
# if needed, pass your credentials before this call
$mech->get($url);
die "Couldn't fetch page" unless $mech->success;
# find all the links that have urls to kmz files
my @links = $mech->find_all_links( url_regex => qr/(?:\.|%2E)kmz$/i );
foreach my $link (@links){ # (loop example)
# use absolute URL path of the link to download file to destination
$mech->get($link->url_abs, ':content_file' => $dest);
last; # only need one (for testing)
}
}
main();