views:

97

answers:

3

I have updated my code to look like this. When i run it though it says it cannot find the specified link. Also what is a good way to test that it is indeed connecting to the page?

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

my $mech = WWW::Mechanize->new();
my $browser = LWP::UserAgent->new;

$browser->credentials(
    'Apache/2.2.3 (CentOS):80',
    'datawww2.wxc.com',
    '************' => '*************'
);

my $response = $browser->get(
'http://datawww2.wxc.com/kml/echo/MESH_Max_180min/'
);

$mech->follow_link( n => 8);

(Original Post) What is the best way to download small files with perl. I looked on CPAN and found lwp-download but it seems to only download from the link. I have a page with links that change every thirty minutes with the date and time in the name so they are never the same. Is there a built in function I can use? Everyone on google keeps saying to use wget, but I was kind of wanting to stick with perl if possible just to help me learn it better while I program with it. Also there is a user name and password to log into the site. I know how to access the site using perl still, but I thought that might change what I can use to download with.

+3  A: 

As stated in a comment in your other question: here

You can use the same method to retrieve .csv files as .html, or any other text-based file for the matter.

#!/usr/bin/perl -w
use strict;
use LWP::Simple;

my $csv = get("http://www.spc.noaa.gov/climo/reports/last3hours_hail.csv")
           or die "Could not fetch NWS CSV page.";

To login, you may need to use WWW::Mechanize to fill out the webform (look at $mech->get(), $mech->submit_form(), and $mech->follow_link())

vol7ron
This is actually for something a little different than that page. On this page the links are updated every thirty minutes and the names are always different since they are a time and date. So I cant just put in the link name
shinjuo
you'll want to use `WWW::Mechanize` then. `$mech->follow_link` can follow the n'th link, or a link with specified text, or a link that matches a regex.
vol7ron
Will mechanize work with a popup login? http://datawww2.wxc.com/kml/echo/MESH_Max_180min/
shinjuo
You may have to do something like: `http://username:password@ datawww2.wxc.com/kml/echo/MESH_Max_180min`, otherwise you might have to capture the cookie. I don't know how you came across that link, so I couldn't tell you.
vol7ron
This is a link to a weather reports site. I was given the link user name and password. So I always just go straight to the link
shinjuo
Try the link with the url in the format as shown above, feeding the username/password beforehand (`username:password@`). Otherwise you have to pass the authentication somehow.
vol7ron
Try the link with the url in the format as shown above, feeding the username/password beforehand (username:password@), **Note:** this will send your username/password in clear text through the protocol. An alternative to try is: `my $mech = WWW::Mechanize->new(autocheck => 1);` `$mech->credentials('username','password');` `$mech->get($url);` see here: http://search.cpan.org/~petdance/WWW-Mechanize-1.54/lib/WWW/Mechanize/Cookbook.pod#Fetch_a_password-protected_page
vol7ron
did everything work out, or did you start a new question?
vol7ron
+2  A: 

Basically, you need to fetch the page, parse it to get the URL, and then download the file.

Personally, I'd use HTML::TreeBuilder::XPath, write a quick XPath expression to go straight to the correct href attribute node, and then plug that into LWP.

use HTML::TreeBuilder::XPath;
my $tree = HTML::TreeBuilder::XPath->new;
$tree->parse({put page content here});
foreach($tree->findnodes({put xpath expression here}){
    {download the file}
}
Anon.
+1  A: 

Does this need to be in perl? Can you just tell wget to download all files linked to from the given page?

Daenyth
I am choosing to do this in perl to help learn perl in the process. I know wget is easier since when you google perl download files everyone just suggets wget. I know it can be done in perl though and I intend on learning how to do it.
shinjuo
That's fine, and I know the feeling. Sometimes doing things the hard way is important for learning how it all works :)
Daenyth