top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Perl: How can i download any format file (.zip or .pdf or whatever content ) from an URL ?

+1 vote
984 views

Easiest way of doing this please ?

posted May 8, 2014 by Yogeshwar Thakur

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

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;
answer May 9, 2014 by Karan Joglekar
Similar Questions
+1 vote

I have tried to install module PDF::FromHTML from cpan but not able to get it.
Please let me know any other module to do the same. please help

+2 votes

Is there any Bugzilla API to upload and download bug's attachment files with perl script?
I want to write a Perl script to download all the attachment in a specified bug, and save the files to local directory.
Attachment method in Bugzilla::Webservice::Bug seems not to download or upload attachment from or to bug.

...