use warnings;
use LWP 5.64;
use URI;
use HTML::LinkExtor;
my $base = "http://search.ebay.com/ws/search/SaleSearch";
my $browser = LWP::UserAgent->new;
my $url = URI->new($base);
$url->query_form(
'satitle'=> 'learning perl',
'sacat'=> 267,
);
my %data = ();
# set up the link handler sub
my $link_extor = HTML::LinkExtor->new(\&handle_links);
#get search results
my $response = $browser->get($url);
#get the links
$link_extor->parse($response->content);
exit;
######################################
sub handle_links {
my ($tag, %links)=@_;
if ($tag eq 'a') {
foreach my $key (keys %links) {
#search for links with Viewitem
if ($key eq 'href') {
if ( $links{$key} =~ m/ViewItem/) {
#get the item number from the link
$links{$key} =~ m/item=(\d+)/;
printf "links{%s} = %s\n",$key,defined $links{$key} ? $links{$key} : 'undef';
if (!defined $1) {
die "\$1 is undefined.";
}
$data{$1}=$links{$key};
}
}
}
}
}
####
links{href} = http://cgi.ebay.com/Learning-Perl-by-Randal-L-Schwartz-1997_W0QQitemZ220021417901QQihZ012QQcategoryZ2228QQssPageNameZWDVWQQrdZ1QQcmdZViewItem
$1 is undefined. at test.pl line 43.