You are in: > Home > Developers Guides
Perl - Downloading a URL with a script

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;
Related articles
- Perl - Downloading a URL with a script - [22nd Mar 2006]
- Perl - Pattern Matching (Regular Expressions) - [12th Jan 2006]
- Shell Scripting - Reading Variables From STDIN - [11th Jan 2006]
- Perl - Configuring and Starting the mod_perl Server - [06th Jan 2006]
- Perl - Installing mod_perl in Three Steps - [06th Jan 2006]
- Perl - mod_perl - [06th Jan 2006]
- Perl - Associative Arrays - [06th Jan 2006]
- Perl - Manipulating Arrays - [06th Jan 2006]
- Perl - Arrays - [06th Jan 2006]
- Perl - Operators - [06th Jan 2006]
- Perl - Scalar Variables - [06th Jan 2006]
- Shell Scripting - Introduction - [08th Dec 2005]
- Shell Scripting - Invoking Shell Scripts - [08th Dec 2005]
- Shell Scripting - Variables - [08th Dec 2005]
- Shell Scripting - Referencing Variables - [08th Dec 2005]
- Shell Scripting - Command Arguments - [08th Dec 2005]
- Shell Scripting - Language Constructs - [08th Dec 2005]
- Perl - Introduction - [06th Dec 2005]
- MySQL - Connecting to the server - [06th Dec 2005]
- MySQL - Introduction - [06th Dec 2005]







