| 1 | package WWW::Baidu; | ||
| 2 | |||
| 3 | use 5.008001; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | use WWW::Mechanize::Cached; | ||
| 8 | use HTML::TreeBuilder; | ||
| 9 | #use YAML::Syck; | ||
| 10 | use Encode qw/ encode decode /; | ||
| 11 | use WWW::Baidu::Record; | ||
| 12 | use Carp 'croak'; | ||
| 13 | use utf8; | ||
| 14 | |||
| 15 | our $VERSION = '0.06'; | ||
| 16 | |||
| 17 | our $Debug = 1; | ||
| 18 | |||
| 19 | sub new ($@) { | ||
| 20 | my $class = ref $_[0] ? ref shift : shift; | ||
| 21 | my $cache; | ||
| 22 | if (@_ == 1 and ref $_[0]) { # for backward-compatibility | ||
| 23 | $cache = shift; | ||
| 24 | } else { | ||
| 25 | my %opts = @_; | ||
| 26 | $cache = delete $opts{cache}; | ||
| 27 | if (%opts) { | ||
| 28 | croak "WWW::Baid::new: Unknown options ", join(' ', keys %opts); | ||
| 29 | } | ||
| 30 | } | ||
| 31 | bless { | ||
| 32 | current => 0, | ||
| 33 | limit => undef, | ||
| 34 | agent => WWW::Mechanize::Cached->new( autocheck => 1, cache => $cache ), | ||
| 35 | records => [], | ||
| 36 | }, $class; | ||
| 37 | } | ||
| 38 | |||
| 39 | sub limit ($$) { | ||
| 40 | my ($self, $count) = @_; | ||
| 41 | $self->{limit} = $count; | ||
| 42 | } | ||
| 43 | |||
| 44 | sub search ($@) { | ||
| 45 | my $self = shift; | ||
| 46 | my $keys = join ' ', @_; | ||
| 47 | $self->{current} = 0; | ||
| 48 | $self->{records} = []; | ||
| 49 | my $agent = $self->{agent}; | ||
| 50 | $agent->env_proxy(); | ||
| 51 | $agent->get('http://www.baidu.com'); | ||
| 52 | if (!$agent->forms or !scalar @{$agent->forms}) { | ||
| 53 | die "Can't find forms in the baidu home page"; | ||
| 54 | } | ||
| 55 | $agent->field( wd => _($keys) ); | ||
| 56 | $agent->submit(); | ||
| 57 | my $content = $agent->content; | ||
| 58 | my $pat = _('百度一下,找到相关网页.*?(\d+(?:\,\d+)*)\s*篇'); | ||
| 59 | my ($count) = ($content =~ /$pat/); | ||
| 60 | if (!defined $count) { return 0; } | ||
| 61 | $count =~ s/,//g; | ||
| 62 | $self->_extract_items($content); | ||
| 63 | return $count; | ||
| 64 | } | ||
| 65 | |||
| 66 | sub next ($) { | ||
| 67 | my $self = shift; | ||
| 68 | my $records = $self->{records}; | ||
| 69 | my $limit = $self->{limit}; | ||
| 70 | if (defined $limit and $self->{current} >= $limit) { return undef; } | ||
| 71 | if (my $item = shift @$records) { | ||
| 72 | $self->{current}++; | ||
| 73 | return $item; | ||
| 74 | } | ||
| 75 | $self->_goto_next_page(); | ||
| 76 | $self->{current}++; | ||
| 77 | shift @$records; | ||
| 78 | } | ||
| 79 | |||
| 80 | sub _goto_next_page ($) { | ||
| 81 | my ($self) = @_; | ||
| 82 | my $i = 0; | ||
| 83 | my $agent = $self->{agent}; | ||
| 84 | my $link = $agent->find_link( text => _('下一页') ); | ||
| 85 | if ($link) { | ||
| 86 | #warn "found!\n" if $Debug; | ||
| 87 | $agent->follow_link(url => $link->url); | ||
| 88 | $self->_extract_items($agent->content); | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | sub _extract_items ($$) { | ||
| 93 | my ($self, $html) = @_; | ||
| 94 | $html = decode('GBK', $html); | ||
| 95 | my $tree = HTML::TreeBuilder->new; | ||
| 96 | $tree->parse($html); | ||
| 97 | $tree->eof; | ||
| 98 | my @td = $tree->look_down( | ||
| 99 | _tag => 'td', | ||
| 100 | class => 'f', | ||
| 101 | ); | ||
| 102 | #warn "!!! ", scalar(@td), " !!!\n"; | ||
| 103 | my $records = $self->{records}; | ||
| 104 | foreach my $td (@td) { | ||
| 105 | push @$records, $self->_parse_record($td); | ||
| 106 | } | ||
| 107 | $tree = $tree->delete; | ||
| 108 | } | ||
| 109 | |||
| 110 | sub _parse_record ($$) { | ||
| 111 | my ($self, $td) = @_; | ||
| 112 | my $title_a = $td->look_down( | ||
| 113 | _tag => 'a', | ||
| 114 | target => '_blank', | ||
| 115 | ); | ||
| 116 | $title_a->detach; | ||
| 117 | my $title = $title_a->as_text; | ||
| 118 | my $cache_a = $td->look_down( | ||
| 119 | _tag => 'a', | ||
| 120 | target => '_blank', | ||
| 121 | sub { $_[0]->as_text =~ /百度快照/ } | ||
| 122 | ); | ||
| 123 | if (defined $cache_a) { | ||
| 124 | $cache_a->detach; | ||
| 125 | } | ||
| 126 | my $text = $td->as_text; | ||
| 127 | if (!$cache_a and $text =~ s/^【[A-Za-z]+】//s) { | ||
| 128 | #warn "<<<< $& >>>>>"; | ||
| 129 | $title = $& . $title; | ||
| 130 | } | ||
| 131 | my ($size, $date); | ||
| 132 | if ($text =~ | ||
| 133 | s/\s+(\d+[KM])\s+(\d{4}-\d{1,2}-\d{1,2})\b//s) { | ||
| 134 | ($size, $date) = ($1, $2); | ||
| 135 | } | ||
| 136 | WWW::Baidu::Record->new( | ||
| 137 | { | ||
| 138 | title => _($title), | ||
| 139 | url => _($title_a->attr('href')), | ||
| 140 | summary => _($text), | ||
| 141 | size => _($size), | ||
| 142 | date => _($date), | ||
| 143 | cached_url => $cache_a ? _($cache_a->attr('href')) : undef, | ||
| 144 | } | ||
| 145 | ); | ||
| 146 | } | ||
| 147 | |||
| 148 | sub _ { | ||
| 149 | my $s = shift; | ||
| 150 | utf8::is_utf8($s) ? encode('GBK', $s) : $s; | ||
| 151 | } | ||
| 152 | |||
| 153 | 1; | ||
| 154 | __END__ |