lv211 has asked for the wisdom of the Perl Monks concerning the following question:
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; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Scraping Ebay
by imp (Priest) on Aug 31, 2006 at 06:01 UTC | |
|
Re^2: Scraping Ebay
by merlyn (Sage) on Aug 31, 2006 at 11:49 UTC | |
|
Re: Scraping Ebay
by zshzn (Hermit) on Aug 31, 2006 at 05:53 UTC | |
|
Re: Scraping Ebay
by Asim (Hermit) on Sep 01, 2006 at 16:12 UTC | |
|
Re: Scraping Ebay
by lv211 (Beadle) on Sep 01, 2006 at 01:56 UTC |