LWP request example

Shows how to load a text file

Another good resource: Web Client Programming with Perl (oreilly.com).

Snippet information

Author:
Jonas

License:
Public Domain

Language:
Perl

Created:
05/29/2006

Updated:
05/29/2006

Tags:
, ,


# load LWP library:
use LWP::UserAgent;
use HTML::Parse;
 
# define a URL
my $url = 'http://www.jonasjohn.de';
 
# create UserAgent object
my $ua = new LWP::UserAgent;
 
# set a user agent (browser-id)
# $ua->agent('Mozilla/5.5 (compatible; MSIE 5.5; Windows NT 5.1)');
 
# timeout:
$ua->timeout(15);
 
# proceed the request:
my $request = HTTP::Request->new('GET');
$request->url($url);
 
my $response = $ua->request($request);
 
 
#
# responses:
#
 
# response code (like 200, 404, etc)
my $code = $response->code;
 
# headers (Server: Apache, Content-Type: text/html, ...)
my $headers = $response->headers_as_string;
 
# HTML body:
my $body =  $response->content;
 
 
 
# print the website content:
# print $body;
 
 
# do some parsing:
 
my $parsed_html = HTML::Parse::parse_html($body);
for (@{ $parsed_html->extract_links(qw(a body img)) }) {
 
    # extract all links (a, body, img)
    my ($link) = @$_;
 
    # print link:
    print $link . "\n";
}


Found a bug? Or do you have a better solution for this?
Feel free to leave a message:

Add a comment


Leave a comment