-
Notifications
You must be signed in to change notification settings - Fork 2
/
grabSearchResults3.pl
executable file
·201 lines (153 loc) · 5.1 KB
/
grabSearchResults3.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/perl -w
# Example code from Chapter 1 of /Perl and LWP/ by Sean M. Burke
# http://www.oreilly.com/catalog/perllwp/
#require 5;
use strict;
#use warnings;
use Text::CSV;
use LWP::Simple;
use HTML::Tree;
use HTML::TreeBuilder;
use WWW::Mechanize;
use URI::Escape;
my $i;
my $myURI;
my $filename;
if (scalar(@ARGV) != 2) {
print "Usage: grabSearchResults.pl filename outdir\n";
print "\tGet doctor search results by input csv file and output directory\n";
exit;
}
my $infile = $ARGV[0];
my $outfolder = $ARGV[1];
my @doctorInfo = @{readCSV($infile)};
# foreach my $docRef (@doctorInfo) {
# my %docs = %$docRef;
# foreach my $key (keys(%docs)) {
# print "$key -> $docs{$key}\t";
# }
# print "\n";
# }
foreach my $docRef (@doctorInfo) {
my $cnt = 0;
my $done = 0;
$myURI = generateSearchUri($docRef, 1);
my %doc = %$docRef;
$filename = "$outfolder/$doc{'SN'}.1.html";
# Get the first page and its links, then the next page, then pause.
#open(FO, ">" . $filename) or die "Cannot open file $filename $!";
#my $content = get($myURI);
my $mech = new WWW::Mechanize;
$mech->get($myURI);
$mech->save_content($filename);
print "Wrote $myURI to $filename\n";
#close(FO);
getSubLinks($filename, $outfolder, "$doc{'SN'}.1");
sleep 15;
# second page!
$myURI = generateSearchUri($docRef, 2);
$mech = new WWW::Mechanize;
$filename = "$outfolder/$doc{'SN'}.2.html";
# Get the first page and its links, then the next page, then pause.
#open(FO, ">" . $filename) or die "Cannot open file $filename $!";
#$content = get($myURI);
$mech->get($myURI);
#print FO $content;
$mech->save_content($filename);
print "Wrote $myURI to $filename\n";
#close(FO);
getSubLinks($filename, $outfolder, "$doc{'SN'}.2");
sleep 30;
}
print "done! Ta Da \n";
sub generateSearchUri {
my $docRef = shift;
my %doc = %$docRef;
my $page = shift;
my $uri = URI->new("http://www.google.com/search");
if ($page == 2) {
$uri-> query_form(ie => "UTF-8", q => "$doc{'lastname'} $doc{'firstname'} $doc{'Specialty'} $doc{'city'} $doc{'State'}", start => "10");
#$uri-> query_form(ie => "UTF-8", q => "$doc{'lastname'} $doc{'firstname'} $doc{'specialty'} $doc{'state'}", start => "10");
} else {
$uri-> query_form(ie => "UTF-8", q => "$doc{'lastname'} $doc{'firstname'} $doc{'Specialty'} $doc{'city'} $doc{'State'}");
#$uri-> query_form(ie => "UTF-8", q => "$doc{'lastname'} $doc{'firstname'} $doc{'specialty'} $doc{'state'}");
}
return $uri;
}
sub getSubLinks {
my $filename = shift;
my $outfolder = shift;
my $filePrepend = shift;
my $tree = HTML::Tree->new_from_file($filename);
my $googleResults = $tree->look_down('id', 'res');
my @resultHeaders = $googleResults->look_down('class', 'r');
print "result headers: " . scalar(@resultHeaders) . "\n";
my $cnt = 1;
foreach my $resHeader (@resultHeaders) {
my @links = $resHeader->look_down('_tag', 'a');
my $outfile = "$outfolder/$filePrepend.$cnt.html";
foreach my $link (@links) {
my $url = $link->attr('href');
$url =~ s/^\/url\?q=//;
$url =~ s/&sa=.*$//;
$url =~ s/&sa=.*$//;
$url = uri_unescape($url);
open(FO, ">" . $outfile) or die "Cannot open file $outfile $!";
my $content = get($url);
print FO $content;
print "Wrote $url to $outfile\n";
close(FO);
$cnt++;
sleep 1;
}
}
# Now, grab sponsored links
$cnt = 1;
while (my $link = $tree->look_down('id', 'pa$cnt')) {
my $fullUrl = $link->attr('href');
# fullURL looks like this:
# /aclk?sa=L&ai=C7H3EyL42T5X0CYWi0AGzj8WiCLugwZ4Cw9nC7BfYs6xCCAAQAVC-9-Hp-f____8BYMmGo4fUo4AQyAEBqgQfT9ASt1Qo7KlAvC6f1cdNJrciPPhd-MklbDbcJNh29w&sig=AOD64_02y7Zhs3jnLMjHIys-RInzAMyBeA&adurl=http://cust1537.bidcenter-29.superpages.com/520245/urologist/Google-Myron%2BI%2BMurdoch%2BMd%2BLlc%3Fsource%3Dsearch%26creative%3D6309098707%26keyword%3Durologist
# We need to cut off everything but the adurl property.
my $url = $fullUrl;
$url =~ s/^.*&adurl=//i;
$url = uri_unescape($url);
my $outfile = "$outfolder/$filePrepend.sponsored.$cnt.html";
open(FO, ">" . $outfile) or die "cannot open file $outfile: $!";
my $content = get($url);
print FO $content;
print "Wrote $url to $outfile\n";
close(FO);
$cnt++;
}
}
sub readCSV {
my $infile = shift;
open(IN, "<$infile");
my @results;
my @lines = <IN>;
close(IN);
my $csv = Text::CSV->new();
my @columnNames;
if ($csv->parse($lines[0])) {
@columnNames = $csv->fields();
for (my $i = 1; $i < scalar(@lines); $i++) {
if ($csv->parse($lines[$i])) {
my @cols = $csv->fields();
my %output;
for (my $j = 0; $j < scalar(@cols); $j++) {
$output{$columnNames[$j]} = $cols[$j];
}
push(@results, \%output);
} else {
my $err = $csv->error_input;
print "Failed to parse line: $err";
}
}
} else {
my $err = $csv->error_input;
print "Failed to parse line: $err";
}
return \@results;
}
__END__