| 1 | |||
| 2 | package WWW::Scraper::Google; | ||
| 3 | |||
| 4 | |||
| 5 | ##################################################################### | ||
| 6 | |||
| 7 | require Exporter; | ||
| 8 | @EXPORT = qw(); | ||
| 9 | @EXPORT_OK = qw(trimTags); | ||
| 10 | @ISA = qw(WWW::Scraper Exporter); | ||
| 11 | $VERSION = sprintf("%d.%02d", q$Revision: 1.23 $ =~ /(\d+)\.(\d+)/); | ||
| 12 | |||
| 13 | use Carp (); | ||
| 14 | use WWW::Scraper(qw(3.02 generic_option addURL trimTags)); | ||
| 15 | |||
| 16 | use strict; | ||
| 17 | |||
| 18 | my $scraperRequest = | ||
| 19 | { | ||
| 20 | 'fieldTranslations' => { | ||
| 21 | '*' => { | ||
| 22 | '*' => '*' | ||
| 23 | } | ||
| 24 | }, | ||
| 25 | #http://www.google.com/search?hl=en&lr=&ie=UTF-8&oe=utf-8&safe=active&q=turntable&btnG=Google+Search 'SKIP' => '' | ||
| 26 | 'nativeDefaults' => { | ||
| 27 | 'q' => '', | ||
| 28 | #'as_eq' => 'turntable', | ||
| 29 | #'oe' => 'utf-8', | ||
| 30 | #'as_q' => '', | ||
| 31 | 'lr' => '', | ||
| 32 | 'hl' => 'en', | ||
| 33 | 'btnG' => 'Google Search', | ||
| 34 | 'safe' => 'active', | ||
| 35 | #'as_epq' => 'google com', | ||
| 36 | #'as_sitesearch' => '', | ||
| 37 | #'as_oq' => '', | ||
| 38 | 'ie' => 'UTF-8' | ||
| 39 | }, | ||
| 40 | 'nativeQuery' => 'q', | ||
| 41 | 'url' => 'http://www.google.com/search?', | ||
| 42 | 'cookies' => 0, | ||
| 43 | 'type' => 'QUERY', | ||
| 44 | 'defaultRequestClass' => undef | ||
| 45 | }; | ||
| 46 | |||
| 47 | my $scraperFrame = | ||
| 48 | [ 'HTML', | ||
| 49 | [ | ||
| 50 | [ 'NEXT', '[^>]>Next<' ], # Google keeps changing their formatting, so watch out! | ||
| 51 | [ 'COUNT', '[,0-9]+</b> of about <b>([,0-9]+)</b>'] , | ||
| 52 | [ 'TABLE', '#4' ], | ||
| 53 | [ 'DIV', | ||
| 54 | [ | ||
| 55 | [ 'HIT*', | ||
| 56 | [ | ||
| 57 | [ 'AN', 'url', 'title' ], | ||
| 58 | #[ 'REGEX', '<font size=-1>(.*?)<br>', 'sampleText'], | ||
| 59 | [ 'REGEX', 'Description:(.*?)<br>', 'description'], | ||
| 60 | [ 'REGEX', '<b>...</b>\s*(.*?)<br>', 'description'], | ||
| 61 | [ 'BODY', 'Category:', '<br>', | ||
| 62 | [ | ||
| 63 | [ 'AN', 'categoryURL', 'category' ] | ||
| 64 | ] | ||
| 65 | ], | ||
| 66 | [ 'AN', 'cachedURL', undef ], | ||
| 67 | [ 'AN', 'similarPagesURL', undef ] | ||
| 68 | ] | ||
| 69 | ] | ||
| 70 | ] | ||
| 71 | ] | ||
| 72 | ] | ||
| 73 | ]; | ||
| 74 | |||
| 75 | |||
| 76 | |||
| 77 | |||
| 78 | |||
| 79 | |||
| 80 | sub testParameters { | ||
| 81 | my ($self) = @_; | ||
| 82 | |||
| 83 | if ( ref $self ) { | ||
| 84 | $self->{'isTesting'} = 1; | ||
| 85 | } | ||
| 86 | |||
| 87 | return { | ||
| 88 | 'SKIP' => '' | ||
| 89 | ,'testNativeQuery' => 'search scraper' | ||
| 90 | ,'expectedOnePage' => 9 | ||
| 91 | ,'expectedMultiPage' => 41 | ||
| 92 | ,'expectedBogusPage' => 1 | ||
| 93 | ,'testNativeOptions' => | ||
| 94 | { | ||
| 95 | 'q' => 'turntable', | ||
| 96 | 'lr' => '', | ||
| 97 | 'hl' => 'en', | ||
| 98 | 'btnG' => 'Google Search', | ||
| 99 | 'safe' => 'active', | ||
| 100 | 'ie' => 'UTF-8' | ||
| 101 | } | ||
| 102 | }; | ||
| 103 | } | ||
| 104 | |||
| 105 | |||
| 106 | sub import | ||
| 107 | { | ||
| 108 | my $package = shift; | ||
| 109 | |||
| 110 | my @exports = grep { "HASH" ne ref($_) } @_; | ||
| 111 | my @options = grep { "HASH" eq ref($_) } @_; | ||
| 112 | |||
| 113 | foreach (@options) | ||
| 114 | { | ||
| 115 | if ( $_->{'scraperBaseURL'} ) { | ||
| 116 | $scraperRequest->{'url'} = $_->{'scraperBaseURL'}; | ||
| 117 | } | ||
| 118 | } | ||
| 119 | |||
| 120 | @_ = ($package, @exports); | ||
| 121 | goto &WWW::Scraper::import; | ||
| 122 | } | ||
| 123 | |||
| 124 | # Access methods for the structural declarations of this Scraper engine. | ||
| 125 | sub scraperRequest { $scraperRequest } | ||
| 126 | sub scraperFrame { $_[0]->SUPER::scraperFrame($scraperFrame); } | ||
| 127 | |||
| 128 | 1; | ||
| 129 | |||
| 130 | |||
| 131 | __END__ |