Once you have the libwww-perl
library, LWP.pm
installed, the code is this:
#!/usr/bin/perl
use LWP::Simple;
$url = get 'http://www.websitename.com/';
For instance:
use File::Basename;
use LWP::Simple qw($ua getstore);
use URI;
my $url = URI->new( 'http://www.nseindia.com/content/historical/EQUITIES/2012/MAR/cm23MAR2012bhav.csv.zip' );
$ua->default_headers( HTTP::Headers->new(
Accept => '*/*',
)
);
$ua->agent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/534.54.16 (KHTML, like Gecko) Version/5.1.4 Safari/534.54.16");
my $rc = getstore( $url, basename( $url->path ) );
say "Result is $rc";
It turns out the combination of a user agent string and the Accept header will do it. Typically these problems come down to making your LWP request look just like the request that your browser would send. I use HTTPScoop to watch the browser transactions, but there are plenty of programs that will do the same thing for you.
If things get even this complex, though, I favor Mojo::UserAgent. It's a bit easier to play with the transaction:
use File::Basename;
use Mojo::UserAgent;
use URI;
my $url = URI->new( 'http://www.nseindia.com/content/historical/EQUITIES/2012/MAR/cm23MAR2012bhav.csv.zip' );
my $file = basename( $url->path );
printf "URL: %s\nFile: %s\n", $url, $file;
my $response = Mojo::UserAgent->new->name(
'"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/534.54.16 (KHTML, like Gecko) Version/5.1.4 Safari/534.54.16"'
)->get( $url->as_string, { Accept => '*/*' } )->res;
open my $fh, '>', $file or die "Could not open [$file]: $!";
print $fh $response->body;
printf "Status: %d\n", $response->code;