leestretton.com

You are in: > Home > Developers Guides

Perl - Downloading a URL with a script

Perl Camel

Getting Documents with LWP::Simple

If you just want to access what's at a particular URL, the simplest way to do it is to use LWP::Simple's functions.

You can simply call the get($url) function. This function will try to 'get' the content of that URL's by downloading it. If it works it will return the content, if it doesn't work it will return undef.

A simple script to perform this function is as follows:

#!/usr/bin/perl

use LWP::Simple;

my $gets = get( 'http://example.com/page.cgi' );
print $gets;

You can then do exactly what you want with the contents of this page.

A slightly more complex script, but one which gives you scope for better error handling is as follows:

#!/usr/bin/perl

use LWP::UserAgent;
use LWP::Simple;
use URI::URL;

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

my $url     = 'http://example.com/page.cgi';

# Set the agent to mirror my browser
$browser->agent( "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.12) Gecko/20060202 Fedora/1.0.7-1.2.fc4 Firefox/1.0.7" );

my $response = $browser->get( $url );
  die "Can't get $url -- ", $response->status_line
   unless $response->is_success;

# Should really check that this isn't blank
print $response->content;

Problem with this page? Let us know
© Lee Stretton 2005 | powered by linux powered by php 5 powered by MySQL download rss feeds powered by php 5 valid css