| 1 | |||
| 2 | use strict; | ||
| 3 | |||
| 4 | package WWW::Scraper::Opcode::NEXT; | ||
| 5 | use base qw(WWW::Scraper::Opcode); | ||
| 6 | use vars qw($VERSION); | ||
| 7 | |||
| 8 | # new() Scraper::Opcode | ||
| 9 | # $cls - Op class | ||
| 10 | # $scaffold - current scaffold | ||
| 11 | # $params - ref to array of params in the 'OP()' portion of the scaffold. | ||
| 12 | sub new { | ||
| 13 | my ($cls, $scaffold, $params) = @_; | ||
| 14 | my $self = bless {}; | ||
| 15 | |||
| 16 | $self->{'fieldsCaptured'} = []; | ||
| 17 | $self->{'fieldsDiscovered'} = ['NEXT']; | ||
| 18 | return $self; | ||
| 19 | } | ||
| 20 | |||
| 21 | sub scrape { | ||
| 22 | my ($self, $scraper, $scaffold, $TidyXML, $hit) = @_; | ||
| 23 | |||
| 24 | my (@ary, $dat) = (@$scaffold, ${$TidyXML->asString()}); | ||
| 25 | |||
| 26 | if ( ref $ary[1] ) | ||
| 27 | { | ||
| 28 | my $datParser = $ary[1]; | ||
| 29 | my $url = ${$TidyXML->asString()}; | ||
| 30 | $url = WWW::Scraper::unescape_query($url) if $TidyXML->m_isTidyd(); | ||
| 31 | $scraper->{'_next_url'} = &$datParser($scraper, $hit, $url); | ||
| 32 | print STDERR "NEXT_URL: $scraper->{'_next_url'}\n" if ($scraper->ScraperTrace('N')); | ||
| 33 | } | ||
| 34 | else | ||
| 35 | { | ||
| 36 | # A simple regex will not work here, since the "next" string may often | ||
| 37 | # appear even when there's no <A>...</A> surrounding it. The problem occurs | ||
| 38 | # when there is a <A>...</A> preceding it, *and* following it. Simple regex's | ||
| 39 | # will find the first anchor, even though it's not the HREF for the "next" string. | ||
| 40 | my $next_url_button = $ary[1]; | ||
| 41 | print STDERR "next_url_button: $next_url_button\n" if ($scraper->ScraperTrace('N')); | ||
| 42 | |||
| 43 | while ( 1 ) | ||
| 44 | { | ||
| 45 | my ($sub_string, $attributes) = $TidyXML->getMarkedTextAndAttributes('A'); | ||
| 46 | last unless $sub_string; | ||
| 47 | if ( $sub_string =~ m-$next_url_button-si ) | ||
| 48 | { | ||
| 49 | my $url = $attributes->{'href'}; | ||
| 50 | if ( $url ) { | ||
| 51 | # Well, you learn something every day! | ||
| 52 | if ( my ($newName, $newValue) = ($url =~ m{&(.*?)=(.*)$}) and $url !~ m{\?} ) { | ||
| 53 | $url = $scraper->{'_last_url'}; | ||
| 54 | $url =~ s{&$newName=[^&]*}{}g; # remove any earlier appearance of this parameter. | ||
| 55 | $url .= "&$newName=$newValue"; | ||
| 56 | } | ||
| 57 | my $datParser = $ary[3]; | ||
| 58 | |||
| 59 | $datParser = \&WWW::Scraper::null unless $datParser; | ||
| 60 | $scraper->{'_base_url'} =~ m-^(.*)/.*$-; | ||
| 61 | my $baseURL = $1; | ||
| 62 | $url = new URI::URL(&$datParser($scraper, $hit, $url), $scraper->{'_base_url'}); | ||
| 63 | $url = $url->abs(); | ||
| 64 | $url = WWW::Scraper::unescape_query($url);# if $TidyXML->m_isTidyd(); | ||
| 65 | $scraper->{'_next_url'} = $url; | ||
| 66 | print STDERR "NEXT_URL: $url\n" if ($scraper->ScraperTrace('U')); | ||
| 67 | last; | ||
| 68 | } | ||
| 69 | } | ||
| 70 | } | ||
| 71 | } | ||
| 72 | return undef; | ||
| 73 | } | ||
| 74 | |||
| 75 | |||
| 76 | 1; |