#!/usr/bin/perl -w
use strict;
use HTML::Parser;
use HTTP::Request;
use HTTP::Response;
use LWP::UserAgent;
use URI;
# Create a LWP::UserAgent object which can be used to fetch pages
my $agent = new LWP::UserAgent();
# Create a URI, which represents a location on the WWW
my $uri = new URI ("http://www.perlmonks.com/");
# Create a HTML::Parser, used to process the HTML
my $parser = new HTML::Parser (
api_version => 3,
start_h => [ \&Start, 'tagname,attr' ],
);
# Define a behavior, used by HTML::Parser when it encounters
# the "start" of a tag (i.e. a declaration like '', where
# the tagname would be 'x', as it is always passed as lower-case)
sub Start
{
my ($tagname,$attr) = @_;
if ($tagname eq 'a')
{
print "$tagname href=$attr->{href}\n";
}
}
# Make a download request for the page, which is requesting
# a 'GET' of the URL specified earlier
my $request = new HTTP::Request ( GET => $uri->as_string() );
# Tell the LWP::UserAgent to process this request, and
# capture the response.
my $response = $agent->request ($request);
# Feed the response to the HTML::Parser, which because of
# the customized 'Start' function, will do something useful.
$parser->parse ($response->content());