| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package WWW::Search::Scrape::Google; |
|
2
|
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
25737
|
use warnings; |
|
|
3
|
|
|
|
|
6
|
|
|
|
3
|
|
|
|
|
104
|
|
|
4
|
3
|
|
|
3
|
|
19
|
use strict; |
|
|
3
|
|
|
|
|
4
|
|
|
|
3
|
|
|
|
|
97
|
|
|
5
|
3
|
|
|
3
|
|
1175
|
use Data::Dumper; |
|
|
3
|
|
|
|
|
11799
|
|
|
|
3
|
|
|
|
|
162
|
|
|
6
|
|
|
|
|
|
|
|
|
7
|
3
|
|
|
3
|
|
20
|
use Carp; |
|
|
3
|
|
|
|
|
5
|
|
|
|
3
|
|
|
|
|
178
|
|
|
8
|
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
#use LWP::UserAgent; |
|
10
|
3
|
|
|
3
|
|
4692
|
use HTML::TreeBuilder; |
|
|
3
|
|
|
|
|
149784
|
|
|
|
3
|
|
|
|
|
52
|
|
|
11
|
3
|
|
|
3
|
|
4438
|
use WWW::Mechanize; |
|
|
3
|
|
|
|
|
606575
|
|
|
|
3
|
|
|
|
|
127
|
|
|
12
|
3
|
|
|
3
|
|
3169
|
use HTML::TreeBuilder::XPath; |
|
|
3
|
|
|
|
|
116658
|
|
|
|
3
|
|
|
|
|
75
|
|
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=head1 NAME |
|
15
|
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
WWW::Search::Scrape::Google - Google search engine |
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=head1 VERSION |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
Version 0.08 |
|
21
|
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
=cut |
|
23
|
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
our $VERSION = '0.08'; |
|
25
|
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
27
|
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
You are not expected to use this module directly. Please use WWW::Search::Scrape instead. |
|
29
|
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
=cut |
|
31
|
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
=head1 FUNCTIONS |
|
33
|
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
=head2 search |
|
35
|
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
search is the most important function in this module. |
|
37
|
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
Inputs |
|
39
|
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
+---------------------------+ |
|
41
|
|
|
|
|
|
|
| keyword | |
|
42
|
|
|
|
|
|
|
+---------------------------+ |
|
43
|
|
|
|
|
|
|
| desired number of results | |
|
44
|
|
|
|
|
|
|
+---------------------------+ |
|
45
|
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
Actually there is another optional argument, content, which is used in debug/test. It will replace LWP::UserAgent. |
|
47
|
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
=cut |
|
49
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
our $frontpage = 'http://www.google.com/ncr'; |
|
51
|
|
|
|
|
|
|
our $geo_location = ''; |
|
52
|
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub search($$;$) |
|
54
|
|
|
|
|
|
|
{ |
|
55
|
5
|
|
|
5
|
1
|
89
|
my ($keyword, $results_num, $content) = @_; |
|
56
|
|
|
|
|
|
|
### search google using |
|
57
|
|
|
|
|
|
|
### keyword: $keyword |
|
58
|
|
|
|
|
|
|
### results number: $results_num |
|
59
|
|
|
|
|
|
|
### content: $content |
|
60
|
|
|
|
|
|
|
|
|
61
|
5
|
|
|
|
|
10
|
my $num = 0; |
|
62
|
|
|
|
|
|
|
|
|
63
|
5
|
50
|
|
|
|
30
|
if ($results_num >= 100) { |
|
64
|
0
|
|
|
|
|
0
|
carp 'WWW::Search::Scrape::Google can not process results more than 100.'; |
|
65
|
0
|
|
|
|
|
0
|
return undef; |
|
66
|
|
|
|
|
|
|
} |
|
67
|
|
|
|
|
|
|
|
|
68
|
5
|
|
|
|
|
9
|
my @res; |
|
69
|
|
|
|
|
|
|
|
|
70
|
5
|
50
|
|
|
|
16
|
unless ($content) |
|
71
|
|
|
|
|
|
|
{ |
|
72
|
5
|
|
|
|
|
68
|
my $mech = WWW::Mechanize->new(agent => 'NotWannaTellYou', cookie_jar => {}); |
|
73
|
5
|
|
|
|
|
39689
|
$mech->get($frontpage); |
|
74
|
|
|
|
|
|
|
#$mech->dump_forms; |
|
75
|
5
|
|
|
|
|
1350112
|
$mech->submit_form( |
|
76
|
|
|
|
|
|
|
form_number => 1, |
|
77
|
|
|
|
|
|
|
fields => { |
|
78
|
|
|
|
|
|
|
q => $keyword, |
|
79
|
|
|
|
|
|
|
num => $results_num, |
|
80
|
|
|
|
|
|
|
start => 0, |
|
81
|
|
|
|
|
|
|
gl => $geo_location |
|
82
|
|
|
|
|
|
|
}, |
|
83
|
|
|
|
|
|
|
button => 'btnG'); |
|
84
|
5
|
50
|
|
|
|
1646150
|
if ($mech->success) { |
|
85
|
5
|
|
|
|
|
124
|
$content = $mech->response()->decoded_content(); |
|
86
|
|
|
|
|
|
|
} |
|
87
|
|
|
|
|
|
|
} |
|
88
|
|
|
|
|
|
|
|
|
89
|
5
|
50
|
|
|
|
11703
|
if (! $content) |
|
90
|
|
|
|
|
|
|
{ |
|
91
|
0
|
|
|
|
|
0
|
carp 'Failed to get content.'; |
|
92
|
0
|
|
|
|
|
0
|
return undef; |
|
93
|
|
|
|
|
|
|
} |
|
94
|
|
|
|
|
|
|
|
|
95
|
5
|
|
|
|
|
88
|
my $tree = HTML::TreeBuilder::XPath->new; |
|
96
|
5
|
|
|
|
|
1742
|
$tree->parse($content); |
|
97
|
5
|
|
|
|
|
575009
|
$tree->eof; |
|
98
|
|
|
|
|
|
|
|
|
99
|
5
|
|
|
|
|
30858
|
my $num_str = $tree->findvalue('//div[@id="resultStats"]'); |
|
100
|
5
|
50
|
|
|
|
578875
|
if ($num_str =~ /([\d,]+)/) { |
|
101
|
5
|
|
|
|
|
27
|
$num = $1; |
|
102
|
5
|
|
|
|
|
50
|
$num =~ s/,//g; |
|
103
|
|
|
|
|
|
|
} |
|
104
|
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
# parse Google returned number |
|
106
|
5
|
|
|
|
|
51
|
@res = $tree->findvalues('//li[@class="g"]/h3/a/@href'); |
|
107
|
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
### Result: @res |
|
109
|
5
|
|
|
|
|
562562
|
return {num => $num, results => \@res}; |
|
110
|
|
|
|
|
|
|
} |
|
111
|
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
1; |