line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package WWW::Search::Scrape::Bing; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
20285
|
use warnings; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
113
|
|
4
|
3
|
|
|
3
|
|
18
|
use strict; |
|
3
|
|
|
|
|
9
|
|
|
3
|
|
|
|
|
100
|
|
5
|
|
|
|
|
|
|
|
6
|
3
|
|
|
3
|
|
16
|
use Carp; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
229
|
|
7
|
3
|
|
|
3
|
|
1280
|
use WWW::Mechanize; |
|
3
|
|
|
|
|
221877
|
|
|
3
|
|
|
|
|
92
|
|
8
|
3
|
|
|
3
|
|
1464
|
use HTML::TreeBuilder; |
|
3
|
|
|
|
|
36167
|
|
|
3
|
|
|
|
|
34
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
# use Smart::Comments; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
=head1 NAME |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
WWW::Search::Scrape::Bing - Bing search engine |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
=head1 VERSION |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
Version 0.08 |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=cut |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
our $VERSION = '0.08'; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
=head1 SYNOPSIS |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
You are not expected to use this module directly. Please use WWW::Search::Scrape instead. |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
=cut |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
=head1 FUNCTIONS |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
=head2 search |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
search is the most important function in this module. |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
Inputs |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
+---------------------------+ |
39
|
|
|
|
|
|
|
| keyword | |
40
|
|
|
|
|
|
|
+---------------------------+ |
41
|
|
|
|
|
|
|
| desired number of results | |
42
|
|
|
|
|
|
|
+---------------------------+ |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
Actually there is another optional argument, content, which is used in debug/test. It will replace LWP::UserAgent. |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
=cut |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub search($$;$) |
50
|
|
|
|
|
|
|
{ |
51
|
2
|
|
|
2
|
1
|
106
|
my ($keyword, $results_num, $content) = @_; |
52
|
|
|
|
|
|
|
### search Bing using |
53
|
|
|
|
|
|
|
### keyword: $keyword |
54
|
|
|
|
|
|
|
### results number: $results_num |
55
|
|
|
|
|
|
|
### content: $content |
56
|
|
|
|
|
|
|
|
57
|
2
|
|
|
|
|
4
|
my $num = 0; |
58
|
|
|
|
|
|
|
|
59
|
2
|
50
|
|
|
|
12
|
if ($results_num > 50) { |
60
|
0
|
|
|
|
|
0
|
carp 'WWW::Search::Scrape::Bing can not process results more than 50.'; |
61
|
0
|
|
|
|
|
0
|
return undef; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
2
|
|
|
|
|
4
|
my @res; |
65
|
|
|
|
|
|
|
|
66
|
2
|
50
|
|
|
|
11
|
unless ($content) |
67
|
|
|
|
|
|
|
{ |
68
|
2
|
|
|
|
|
78
|
my $mech = WWW::Mechanize->new(cookie_jar => {}); |
69
|
2
|
|
|
|
|
27371
|
$mech->agent_alias('Windows IE 6'); |
70
|
2
|
|
|
|
|
170
|
$mech->get('http://www.bing.com/?mkt=en-us'); |
71
|
|
|
|
|
|
|
#$mech->dump_links(); |
72
|
2
|
|
|
|
|
522140
|
$mech->follow_link(url_regex => qr/^\/settings.aspx/); |
73
|
|
|
|
|
|
|
#$mech->dump_forms; |
74
|
0
|
|
|
|
|
|
$mech->submit_form( |
75
|
|
|
|
|
|
|
form_number => 1, |
76
|
|
|
|
|
|
|
fields => { |
77
|
|
|
|
|
|
|
rpp => '50', |
78
|
|
|
|
|
|
|
sl => '40', |
79
|
|
|
|
|
|
|
setplang => 'en-US', |
80
|
|
|
|
|
|
|
langall => '0', |
81
|
|
|
|
|
|
|
}); |
82
|
|
|
|
|
|
|
#$mech->dump_forms; |
83
|
0
|
|
|
|
|
|
$mech->submit_form(form_number => 1, |
84
|
|
|
|
|
|
|
fields => { |
85
|
|
|
|
|
|
|
q => $keyword, |
86
|
|
|
|
|
|
|
}); |
87
|
|
|
|
|
|
|
#print $mech->uri, "\n"; |
88
|
|
|
|
|
|
|
#print $mech->title; |
89
|
0
|
|
|
|
|
|
$content = $mech->response->decoded_content; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
0
|
|
|
|
|
|
my $tree = HTML::TreeBuilder->new; |
93
|
0
|
|
|
|
|
|
$tree->parse($content); |
94
|
0
|
|
|
|
|
|
$tree->eof; |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
# parse Bing returned number |
97
|
|
|
|
|
|
|
{ |
98
|
0
|
|
|
|
|
|
my ($xx) = $tree->look_down('_tag', 'span', |
99
|
|
|
|
|
|
|
sub |
100
|
|
|
|
|
|
|
{ |
101
|
0
|
0
|
0
|
0
|
|
|
return unless $_[0]->attr('class') && $_[0]->attr('class') eq 'sb_count'; |
102
|
0
|
|
|
|
|
|
}); |
103
|
0
|
0
|
|
|
|
|
return {num => 0, results => undef} unless $xx; |
104
|
|
|
|
|
|
|
|
105
|
0
|
|
|
|
|
|
my @r = $xx->content_list; |
106
|
0
|
|
|
|
|
|
my ($number) = $r[0] =~ /of ([\d,]+) res/; |
107
|
0
|
|
|
|
|
|
$num = join('', split(',', $number)); |
108
|
|
|
|
|
|
|
### Bing returns: $num |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
0
|
|
|
|
|
|
my @x = $tree->look_down('_tag', 'h3'); |
112
|
|
|
|
|
|
|
|
113
|
0
|
|
|
|
|
|
foreach (@x) { |
114
|
0
|
|
|
|
|
|
my ($link) = $_->look_down('_tag', 'a'); |
115
|
|
|
|
|
|
|
|
116
|
0
|
0
|
|
|
|
|
if ($link) { |
117
|
0
|
0
|
|
|
|
|
push @res, $link->attr('href') unless $link->attr('href') =~ /^\//; |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
### Result: @res |
122
|
0
|
|
|
|
|
|
return {num => $num, results => \@res}; |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|