# Strict
use strict;
use warnings;
# Libraries
use Data::Dumper;
use LWP::UserAgent;
use HTML::TreeBuilder;
my $url = 'http://www.somepage.com';
# $browser->cookie_jar({}); #### use if the site requires cookies
my $browser = LWP::UserAgent->new;
my @ns_headers = (
'User-Agent' => 'Mozilla/4.76 [en] (Win98; U)',
'Accept' => 'image/gif, image/x-xbitmap, image/jpeg,
image/pjpeg, image/png, */*',
'Accept-Charset' => 'iso-8859-1,*,utf-8',
'Accept-Language' => 'en-US',
);
my $response = $browser->get($url, @ns_headers);
die "Can't get $url -- ", $response->status_line unless $response->is_success;
die "Hey, I was expecting HTML, not ", $response->content_type
unless $response->content_type eq 'text/html';
# Now get the content, and display it
my $content = $response->content;
print "TFD> content $content\n";
# Now build the HTML tree
my $tree = HTML::TreeBuilder->new_from_content($content);
# Now find each occurrence of the desired tag
my $tag = 'a';
my $match = $tree->find($tag);
if ($match) {
# Found it!
print "Found tag '$tag' ...\n";
print " As text: ", $match->as_text, "\n";
print " As text: ", $match->as_HTML, "\n";
} else {
print "Unable to find tag '$tag'\n";
}
####
print "TFD> content $content\n";
####
my $tree = HTML::TreeBuilder->new_from_content($content);
####
my $tag = 'a';
my $match = $tree->find($tag);
####
print " As text: ", $match->as_text, "\n";
print " As text: ", $match->as_HTML, "\n"