line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package WWW::Search::Scrape::Yahoo; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
10
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
60
|
|
4
|
2
|
|
|
2
|
|
9
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
53
|
|
5
|
|
|
|
|
|
|
|
6
|
2
|
|
|
2
|
|
9
|
use Carp; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
118
|
|
7
|
2
|
|
|
2
|
|
12
|
use WWW::Mechanize; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
49
|
|
8
|
2
|
|
|
2
|
|
11
|
use HTML::TreeBuilder; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
12
|
|
9
|
2
|
|
|
2
|
|
45
|
use URI::Escape; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
975
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
=head1 NAME |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
WWW::Search::Scrape::Yahoo - Yahoo search engine |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 VERSION |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
Version 0.08 |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
=cut |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
our $VERSION = '0.08'; |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
=head1 SYNOPSIS |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
You are not expected to use this module directly. Please use WWW::Search::Scrape instead. |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
=cut |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
=head1 FUNCTIONS |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
=head2 search |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
search is the most important function in this module. |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
Inputs |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
+---------------------------+ |
38
|
|
|
|
|
|
|
| keyword | |
39
|
|
|
|
|
|
|
+---------------------------+ |
40
|
|
|
|
|
|
|
| desired number of results | |
41
|
|
|
|
|
|
|
+---------------------------+ |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=cut |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub search($$;$) |
47
|
|
|
|
|
|
|
{ |
48
|
0
|
|
|
0
|
1
|
|
my ($keyword, $results_num, $content) = @_; |
49
|
|
|
|
|
|
|
### search Yahoo using |
50
|
|
|
|
|
|
|
### keyword: $keyword |
51
|
|
|
|
|
|
|
### results number: $results_num |
52
|
|
|
|
|
|
|
### content: $content |
53
|
|
|
|
|
|
|
|
54
|
0
|
|
|
|
|
|
my $num = 0; |
55
|
|
|
|
|
|
|
|
56
|
0
|
0
|
|
|
|
|
if ($results_num > 100) { |
57
|
0
|
|
|
|
|
|
carp 'WWW::Search::Scrape::Yahoo can not process results more than 100.'; |
58
|
0
|
|
|
|
|
|
return undef; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
0
|
|
|
|
|
|
my @res; |
62
|
|
|
|
|
|
|
|
63
|
0
|
0
|
|
|
|
|
unless ($content) |
64
|
|
|
|
|
|
|
{ |
65
|
|
|
|
|
|
|
#yahoo is funny about search result count. Valid values are: 10,15,20,30,40,100 |
66
|
|
|
|
|
|
|
# set up the value to provide at least the number required |
67
|
0
|
|
|
|
|
|
my $yahoo_result_num; |
68
|
0
|
0
|
|
|
|
|
if ($results_num < 10) { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
69
|
0
|
|
|
|
|
|
$yahoo_result_num = 10; |
70
|
|
|
|
|
|
|
} elsif ($results_num < 15) { |
71
|
0
|
|
|
|
|
|
$yahoo_result_num = 15; |
72
|
|
|
|
|
|
|
} elsif ($results_num < 20) { |
73
|
0
|
|
|
|
|
|
$yahoo_result_num = 20; |
74
|
|
|
|
|
|
|
} elsif ($results_num < 30) { |
75
|
0
|
|
|
|
|
|
$yahoo_result_num = 30; |
76
|
|
|
|
|
|
|
} elsif ($results_num < 40) { |
77
|
0
|
|
|
|
|
|
$yahoo_result_num = 40; |
78
|
|
|
|
|
|
|
} elsif ($results_num < 100) { |
79
|
0
|
|
|
|
|
|
$yahoo_result_num = 100; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
0
|
|
|
|
|
|
my $mech = WWW::Mechanize->new(cookie_jar => {}); |
83
|
0
|
|
|
|
|
|
$mech->agent_alias('Windows Mozilla'); |
84
|
0
|
|
|
|
|
|
$mech->get('http://search.yahoo.com/'); |
85
|
|
|
|
|
|
|
#$mech->dump_forms; |
86
|
0
|
|
|
|
|
|
$mech->submit_form( |
87
|
|
|
|
|
|
|
form_number => 1, |
88
|
|
|
|
|
|
|
fields => { |
89
|
|
|
|
|
|
|
p => $keyword, |
90
|
|
|
|
|
|
|
'sb-top' => 'fr2', |
91
|
|
|
|
|
|
|
ei => 'UTF-8', |
92
|
|
|
|
|
|
|
n => $yahoo_result_num, |
93
|
|
|
|
|
|
|
}); |
94
|
0
|
0
|
|
|
|
|
if ($mech->success) { |
95
|
0
|
|
|
|
|
|
$content = $mech->response->decoded_content; |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
0
|
|
|
|
|
|
my $tree = HTML::TreeBuilder->new; |
100
|
0
|
|
|
|
|
|
$tree->parse($content); |
101
|
0
|
|
|
|
|
|
$tree->eof; |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
# yahoo does not show the total result count |
104
|
|
|
|
|
|
|
|
105
|
0
|
|
|
|
|
|
my @x = $tree->look_down('_tag', 'h3'); |
106
|
|
|
|
|
|
|
|
107
|
0
|
|
|
|
|
|
foreach (@x) { |
108
|
0
|
|
|
|
|
|
my ($link) = $_->look_down('_tag', 'a'); |
109
|
0
|
0
|
|
|
|
|
if ($link) { |
110
|
0
|
|
|
|
|
|
my $obfuscatedHref = $link->attr('href'); |
111
|
0
|
0
|
|
|
|
|
if ($obfuscatedHref =~ /.*\*\*(.*)/) { |
112
|
0
|
|
|
|
|
|
push @res, uri_unescape($1); |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
#if we have reached the max size we originally asked for, leave now |
115
|
0
|
0
|
|
|
|
|
last if (scalar(@res) >= $results_num); |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
0
|
|
|
|
|
|
return {num => $num, results => \@res}; |
122
|
|
|
|
|
|
|
} |