I got this script off the internet for scraping ebay and sending the results to an email addy. Every time I run the script I get the error "use of unitialized hash value".

This is in reference to the %data hash. I've tried playing around with it, but I can't seem to figure out what's wrong.

Here's the code:

use strict; use LWP 5.64; use URI; use HTML::LinkExtor; use HTML::HeadParser; use Net::SMTP; use MIME::Lite; # Set too your country e.g. ebaycom.au my $country = ".com"; my $base = "http://search.ebay".$country."/ws/search/SaleSearch"; # Title to search for my $title = "learning perl"; # Catergory to search get from http://listings.ebay.co.uk my $cat = "267"; #Books #your email address my $email = qw/emailaddy/; #your mail server my $mailsrv = qw/mailserver/; # File to keep items number already seen my $localfile = "listing.txt"; # declare some vars my ($a,$b,$line, $itemnumber,@title,$results,%data,%olditems,$key); #Set hash to nothing %data = (); my $browser = LWP::UserAgent->new; # Un comment if you need to use a proxy - replace with real address an +d port #$browser->proxy(['http', 'ftp'], 'http://10.111.10.11:8080/'); my $url =URI->new($base); $url->query_form( 'satitle'=> $title, 'sacat'=> $cat ); # 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); #get items already seen in hash %olditems %olditems=(); if (-s $localfile) { open (INFILE,"$localfile"); while (<INFILE> ) { chomp; next if $_ eq ""; $olditems{$_}=1; } close (INFILE); } # delete items from %data hash already seen foreach $key (keys %olditems) { if (exists($data{$key})) { delete $data{$key}; } } # *** save any remaining new entries to file *** open (OUTFILE,">>$localfile"); my $mailbody=""; foreach $itemnumber (keys %data) { my $line=&get_title($data{$itemnumber}); print OUTFILE $itemnumber."\n"; #print "Line=".$line."\n"; $mailbody=$mailbody.$line; } close (OUTFILE); #send mail my $msg = MIME::Lite->new ( To => $email, From => $email, Subject =>"Ebay Search for [".$title."]", Type =>'multipart/related' ); $msg->attach(Type => 'text/html', Data => qq{ $mailbody } ); MIME::Lite->send('smtp', $mailsrv, Timeout=>60); $msg->send if $mailbody ne ""; ###################################### sub handle_links { my ($tag, %links)=@_; my $key; if ($tag eq 'a') { foreach $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+)/; $data{$1}=$links{$key}; } } } } } sub get_title($) { my ($page)=@_; my $itempage = LWP::UserAgent->new; my $item_contents=$itempage->get($page); my $p = HTML::HeadParser->new; $p->parse($item_contents->content); my $link="<p><a href=\"$page\">".$p->header('Title')."</p>"; return $link; }

In reply to Scraping Ebay by lv211

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.