carric has asked for the wisdom of the Perl Monks concerning the following question:

Really struggling with what is going on.. I am writing a little spider to grab all the ads off a site and you have to pass a "Count=n" variable in the URL to get more ads. When set to zero, you get the first 10 ads, then set to Count=10 to get the next 10 and so on...
#!/usr/bin/perl use lib qw( ..); use HTML::TableExtract; use LWP::Simple; use Data::Dumper; my $te = new HTML::TableExtract( depth=>3,count=>0); # which table do +we want # Get the total number of records according to MDJONLINE my $content = get( "http://www.mdjonline.com/classifieds/legals/index.inn?loc=detail&main +=Legals&sub=Foreclosures&Count=0") || die "Couldn't get URL\n"; $content =~ m{Total\s?ads: (\d+)} || die "No dice\n"; my $total = $1; # Set some intial values for record number and iterations my $iter = ($total - ($total % 10)); # calculate iterations to nearest + num divisible by 10 my $recnum = 0; # number of start record for next dump while($recnum <= $iter) { my $mdjURL = "http://www.mdjonline.com/classifieds/legals/inde +x.inn?loc=detail&main=Legals&sub=Foreclosures&Count=".$recnum; my $content = get($mdjURL); $te->parse($content); foreach $ts ($te->table_states) { foreach $row ($ts->rows) { print Dumper $row; } } $recnum += 10; }
I separated out the URL as a var so I could debug it and verify that it is INDEED incrementing the URL by 10, and it is, but it returns the same 10 ads until it hits $total, so of course I have 74 identical 10-ad blocks (not what I'm after obviously). I have tried just about everything I can think of and about ready to impail myself with a grapefruit spoon in the front yard.. hmm.. wonder why we call it "grape"frut.. sure as hell doesn't look like a grape.

If anyone out there can help, I would be EVER so appreciative. =)

UPDATE:
I figured it out.
#!/usr/bin/perl use lib qw( ..); use HTML::TableExtract; use LWP::Simple; use Data::Dumper; # Get the total number of records according to MDJONLINE my $content = get( "http://www.mdjonline.com/classifieds/legals/index.inn?loc=detail&main +=Legals&sub=Foreclosures&Count=0") || die "Couldn't get URL\n"; $content =~ m{Total\s?ads: (\d+)} || die "No dice\n"; my $total = $1; # Set some intial values for record number and iterations my $iter = ($total - ($total % 10)); # calculate iterations to nearest + num divisible by 10 my $recnum = 0; # number of start record for next dump while($recnum <= $iter) { my $te = new HTML::TableExtract( depth=>3,count=>0); my $mdjURL = "http://www.mdjonline.com/classifieds/legals/inde +x.inn?loc=detail&main=Legals&sub=Foreclosures&Count=".$recnum; my $content = get($mdjURL); $te->parse($content); foreach $ts ($te->table_states) { foreach $row ($ts->rows) { print Dumper $row; } } $recnum += 10; }
Declaring $te inside the while loop makes it reinitialize each time.

Replies are listed 'Best First'.
Re: HTML::Table::Extract
by Jaap (Curate) on Nov 22, 2003 at 10:53 UTC
    Did you try printing $content in the while loop to verify that you get new page with 10 new ads?

    If so, try to re-initialise $te in the while-loop (for testing only) to see if the problem is the TableExtractor not parsing a 2nd time.

    On a side-note, who do you calculate $iter? I'd just say while ($recnum < $total) {}
      Hmm... as obvious as that is, I assumed if it was a new page, it would be new data.. I DID try printing out the content, but I was too lazy to actually look at the raw HTML.. I really should try that. Good idea...

      I will research re-initializing vars as well.

      As far as why I calc $iter, if I have say 736 ads, that LAST value for $recnum I want is 730 since this displays that last 6 ads. If I incement all the way to 736 I get a blank page, so I divide by 10, take the remainder and subtract that to get the last number evenly divisible by 10.