-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpersonalscraper.html
More file actions
534 lines (383 loc) · 18.3 KB
/
Copy pathpersonalscraper.html
File metadata and controls
534 lines (383 loc) · 18.3 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
<html>
<head>
<script>
/**
* Personal Scraper: Spider emails using YQL
* ---
* @author Anand Chhatpar (http://www.anandvc.com)
* @version 1.1
* @updated 12-MAY-2012
* ---
* @info http://www.anandvc.com/projects/
*/
</script>
<title>Personal Scraper</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
var interactive_mode = "0";
// Return new array with duplicate values removed
Array.prototype.unique =
function() {
var a = [];
var l = this.length;
for(var i=0; i<l; i++) {
for(var j=i+1; j<l; j++) {
// If this[i] is found later in the array
if (this[i] === this[j])
j = ++i;
}
a.push(this[i]);
}
return a;
};
var LINKQUEUE = [];
var REJECTEDLINKS = [];
var EMAILSFOUND = [];
var CURRENTPAGE = 0;
/**
* jQuery.ajax mid - CROSS DOMAIN AJAX
* ---
* @author James Padolsey (http://james.padolsey.com)
* @version 0.11
* @updated 12-JAN-10
* ---
* Note: Read the README!
* ---
* @info http://james.padolsey.com/javascript/cross-domain-requests-with-jquery/
*/
jQuery.ajax = (function(_ajax){
var protocol = location.protocol,
hostname = location.hostname,
exRegex = RegExp(protocol + '//' + hostname),
YQL = 'http' + (/^https/.test(protocol)?'s':'') + '://query.yahooapis.com/v1/public/yql?callback=?',
query = 'select * from html where url="{URL}" and xpath="*"';
function isExternal(url) {
return !exRegex.test(url) && /:\/\//.test(url);
}
return function(o) {
var url = o.url;
if ( /get/i.test(o.type) && !/json/i.test(o.dataType) && isExternal(url) ) {
// Manipulate options so that JSONP-x request is made to YQL
o.url = YQL;
o.dataType = 'json';
o.data = {
q: query.replace(
'{URL}',
url + (o.data ?
(/\?/.test(url) ? '&' : '?') + jQuery.param(o.data)
: '')
),
format: 'xml'
};
// Since it's a JSONP request
// complete === success
if (!o.success && o.complete) {
o.success = o.complete;
delete o.complete;
}
o.success = (function(_success){
return function(data) {
if (_success) {
// Fake XHR callback.
if (!data.results[0]){
data.results[0]="";
}
_success.call(this, {
responseText: data.results[0]
// YQL screws with <script>s
// Get rid of them
.replace(/<script[^>]+?\/>|<script(.|\s)*?\/script>/gi, '')
}, 'success');
}
};
})(o.success);
}
return _ajax.apply(this, arguments);
};
})(jQuery.ajax);
retrylist = [];
function fetchPage(url) {
// trick is to use Ben Alman's simple PHP proxy instead of querying directly with YQL which seems to block certain websites.
url_to_use = url;
// if ($('#yql').val()==1){
// url_to_use = "http://benalman.com/code/projects/php-simple-proxy/ba-simple-proxy.php?url="+url+"&full_headers=1&full_status=1";
// }
// alert("Getting:"+url);
$.ajax({
type: "GET",
url: url_to_use,
error: function(request, status) {
// alert('Retrying after Error fetching ' + url);
if(retrylist[url]>=0){
retrylist[url]=retrylist[url]+1;
}
else{
retrylist[url]=0;
}
if (retrylist[url]<2){
fetchPage(url);
}
else{
// too many retries, move on to the next page
CURRENTPAGE++;
if (CURRENTPAGE<LINKQUEUE.length){
updateLinkQueueTable();
$('#currentpage').html(CURRENTPAGE);
setTimeout(function(){fetchPage(LINKQUEUE[CURRENTPAGE])},2500); // Wait 2.5 seconds before spidering the next page
// CURRENTPAGE++;
}
}
},
success: function(data) {
CURRENTPAGE++;
parse(data.responseText);
}
});
} // end of function fetchPage (url)
function updateLinkQueueTable(){
link_queue_string = "<table>";
for(i=0;i<LINKQUEUE.length;i++){
link_queue_string = link_queue_string + "<tr style='height:28px;'>";
if (i==CURRENTPAGE){
link_queue_string = link_queue_string + "<td style='background-color:lightyellow;'>" + i + "</td><td style='background-color:lightyellow;text-align:center;'><b>»</b></td><td style='background-color:lightyellow;'><b>" + LINKQUEUE[i] + "</b></td>";
}
else if (i>CURRENTPAGE){
link_queue_string = link_queue_string + "<td>" + i + "</td><td><input type='button' onclick='removelink(" + i + ");' value='Remove' name='Remove'></td><td>" + LINKQUEUE[i] + "</td>";
}
else if (i<CURRENTPAGE){
link_queue_string = link_queue_string + "<td>" + i + "</td><td style='color:green;text-align:center;'>✓</td><td>" + LINKQUEUE[i] + "</td>";
}
link_queue_string = link_queue_string + "</tr>";
} // end of for loop through each link
link_queue_string = link_queue_string + "</table>";
$('#linkqueue').html(link_queue_string);
} // end of function updateLinkQueueTable
function parse(data) {
// alert($(data).find("h1").text());
emails_in_page = data.match(/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/gi);
if (emails_in_page){
EMAILSFOUND=EMAILSFOUND.concat(emails_in_page);
EMAILSFOUND=EMAILSFOUND.unique();
$('#output').val(EMAILSFOUND.join("\n"));
$('#emailcount').html(EMAILSFOUND.length);
} // end of if
// ignore everything before the body tag
body_begins_at = data.toLowerCase().indexOf("body");
if (body_begins_at>0){
data=data.substring(body_begins_at,data.length);
}
absolute_links_in_page = data.match(/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/gi);
if (absolute_links_in_page){
// $('#output').html(absolute_links_in_page.join("<br>"));
for(i=0;i<absolute_links_in_page.length;i++){
// check each link to see if it should be added to the queue
//does it start with http? If not, add http:// to it
if (absolute_links_in_page[i].indexOf("http")!=0){
absolute_links_in_page[i] = "http://" + absolute_links_in_page[i];
}
//remove any anchors
if (absolute_links_in_page[i].indexOf("#")>0){
absolute_links_in_page[i] = absolute_links_in_page[i].split("#")[0];
}
//remove trailing slash
if (absolute_links_in_page[i].charAt(absolute_links_in_page[i].length-1)=="/"){
absolute_links_in_page[i] = absolute_links_in_page[i].substring(0,absolute_links_in_page[i].length-1);
}
// if link is of an image or a document or contains quotation marks, skip it
lowercase_link = absolute_links_in_page[i].toLowerCase();
if (lowercase_link.indexOf(".png")>0 || lowercase_link.indexOf(".jpg")>0 || lowercase_link.indexOf(".gif")>0 || lowercase_link.indexOf(".jpeg")>0 || lowercase_link.indexOf(".pdf")>0 || lowercase_link.indexOf(".doc")>0 || lowercase_link.indexOf(".bmp")>0 || lowercase_link.indexOf(".swf")>0 || lowercase_link.indexOf(".cab")>0 || lowercase_link.indexOf(""")>0 || lowercase_link.indexOf("file:")>=0 ){
continue;
}
// does it already exist in the link queue? If not, add it.
// traverse the linkqueue and check each element
alreadyexists = false;
for (j=0;j<LINKQUEUE.length;j++){
if (LINKQUEUE[j]==absolute_links_in_page[i]){
alreadyexists = true;
break;
} // end of if that found a matching url in the queue
} // end of for loop over LINKQUEUE with j
// check reject queue if the link was not found already
if (!alreadyexists){
for (j=0;j<REJECTEDLINKS.length;j++){
if (REJECTEDLINKS[j]==absolute_links_in_page[i]){
alreadyexists = true;
break;
} // end of if that found a matching url in the queue
} // end of for loop over REJECTEDLINKS with j
}
if (!alreadyexists){
LINKQUEUE.push(absolute_links_in_page[i]);
// if (absolute_links_in_page[i].indexOf('about_us')>0)
// alert("Found and Added " + absolute_links_in_page[i] + " and indexOf('\"')=" + absolute_links_in_page[i].indexOf('"'));
// $('#linkqueue').html(LINKQUEUE.slice(CURRENTPAGE).join("<br>"));
}
} // end of for
} // end of if absolute links in page
// find relative links in page, convert them to absolute links and add them to the link queue if they're not already there
// current domain comes from current url
// find the index of the 3rd / to know where to stop the substring to get the domain
current_domain = "";
thirdslash = LINKQUEUE[CURRENTPAGE-1].substring(8,LINKQUEUE[CURRENTPAGE-1].length).indexOf("/")
if (thirdslash==-1){
current_domain = LINKQUEUE[CURRENTPAGE-1];
}
else{
current_domain = LINKQUEUE[CURRENTPAGE-1].substring(0,thirdslash+8);
}
// current path comes from current URL as well
// find the index of the last slash before a ? if it exists, otherwise, add a slash to the current domain to make the current path.
current_path = "";
lastslash = LINKQUEUE[CURRENTPAGE-1].split("?")[0].lastIndexOf("/");
if (lastslash<=8){
current_path = current_domain + "/";
}
else{
current_path = LINKQUEUE[CURRENTPAGE-1].substring(0,lastslash+1);
}
// alert("current_path="+current_path);
//remove whitespace
data=data.replace(/\s/g, "");
while (data.indexOf("href=")>0){
current_href = data.indexOf("href=");
// alert ("current_href=" + current_href);
// alert ("data starts with " + data.substring(0,50));
data = data.substring(current_href+5,data.length);
// alert ("after substringing data, it starts with " + data.substring(0,50));
quote_character = data.charAt(0);
data = data.substring(1,data.length);
current_link = data.substring(0,data.indexOf(quote_character));
// alert ("current_link in this iteration is " + current_link);
// does current link start with http? or javascript: or # or mailto: if so, then we can skip it
if (current_link.substring(0,4).toLowerCase()=="http" || current_link.substring(0,11).toLowerCase()=="javascript:" || current_link.substring(0,1)=="#" || current_link.substring(0,7)=="mailto:"){
data = data.substring(4,data.length);
continue;
}
// does current link start with / if so, then convert it to an absolute link and check for uniqueness before adding
else if (current_link.charAt(0)=="/"){
current_absolute_link = current_domain + current_link;
//remove any anchors
if (current_absolute_link.indexOf("#")>0){
current_absolute_link = current_absolute_link.split("#")[0];
}
//remove trailing slash
if (current_absolute_link.charAt(current_absolute_link.length-1)=="/"){
current_absolute_link = current_absolute_link.substring(0,current_absolute_link.length-1);
}
// if link is of an image or a document, skip it
lowercase_link = current_absolute_link.toLowerCase();
if (lowercase_link.indexOf(".png")>0 || lowercase_link.indexOf(".jpg")>0 || lowercase_link.indexOf(".gif")>0 || lowercase_link.indexOf(".jpeg")>0 || lowercase_link.indexOf(".pdf")>0 || lowercase_link.indexOf(".doc")>0 || lowercase_link.indexOf(".bmp")>0 || lowercase_link.indexOf(".swf")>0 || lowercase_link.indexOf(".cab")>0 || lowercase_link.indexOf(""")>0 || lowercase_link.indexOf("file:")>=0 )
continue;
// does it already exist in the link queue? If not, add it.
// traverse the linkqueue and check each element
alreadyexists = false;
for (j=0;j<LINKQUEUE.length;j++){
if (LINKQUEUE[j]==current_absolute_link){
alreadyexists = true;
break;
} // end of if that found a matching url in the queue
} // end of for loop over LINKQUEUE with j
// check the rejected queue if the url was not found already
if (!alreadyexists){
for (j=0;j<REJECTEDLINKS.length;j++){
if (REJECTEDLINKS[j]==current_absolute_link){
alreadyexists = true;
break;
} // end of if that found a matching url in the queue
} // end of for loop over REJECTEDLINKS with j
}
if (!alreadyexists){
LINKQUEUE.push(current_absolute_link);
// if (current_absolute_link.indexOf('about_us')>0)
// alert("Found " + current_link + " Added " + current_absolute_link + " indexOf('\"')=" + current_absolute_link.indexOf('"') );
// $('#linkqueue').html(LINKQUEUE.slice(CURRENTPAGE).join("<br>"));
}
} // end of else if to check for / at the beginning
else {
// its a relative URL
current_absolute_link = current_path + current_link;
//remove any anchors
if (current_absolute_link.indexOf("#")>0){
current_absolute_link = current_absolute_link.split("#")[0];
}
//remove trailing slash
if (current_absolute_link.charAt(current_absolute_link.length-1)=="/"){
current_absolute_link = current_absolute_link.substring(0,current_absolute_link.length-1);
}
// if link is of an image or a document, skip it
lowercase_link = current_absolute_link.toLowerCase();
if (lowercase_link.indexOf(".png")>0 || lowercase_link.indexOf(".jpg")>0 || lowercase_link.indexOf(".gif")>0 || lowercase_link.indexOf(".jpeg")>0 || lowercase_link.indexOf(".pdf")>0 || lowercase_link.indexOf(".doc")>0 || lowercase_link.indexOf(".bmp")>0 || lowercase_link.indexOf(".swf")>0 || lowercase_link.indexOf(".cab")>0 || lowercase_link.indexOf(""")>0 || lowercase_link.indexOf("file:")>=0 )
continue;
// does it already exist in the link queue? If not, add it.
// traverse the linkqueue and check each element
alreadyexists = false;
for (j=0;j<LINKQUEUE.length;j++){
if (LINKQUEUE[j]==current_absolute_link){
alreadyexists = true;
break;
} // end of if that found a matching url in the queue
} // end of for loop over LINKQUEUE with j
// check the rejected list if the link is not found already
if (!alreadyexists){
for (j=0;j<REJECTEDLINKS.length;j++){
if (REJECTEDLINKS[j]==current_absolute_link){
alreadyexists = true;
break;
} // end of if that found a matching url in the queue
} // end of for loop over REJECTEDLINKS with j
}
if (!alreadyexists){
LINKQUEUE.push(current_absolute_link);
// $('#linkqueue').html(LINKQUEUE.slice(CURRENTPAGE).join("<br>"));
// if (current_absolute_link.indexOf('about_us')>0)
// alert("Found " + current_link + " Added " + current_absolute_link + " indexOf('\"')=" + current_absolute_link.indexOf('"') );
}
} // end of else
} // end of while that looks for hrefs
if (CURRENTPAGE<LINKQUEUE.length){
updateLinkQueueTable();
$('#currentpage').html(CURRENTPAGE);
if (interactive_mode=="1"){
$('#continuebutton').show();
}
else{
setTimeout(function(){fetchPage(LINKQUEUE[CURRENTPAGE])},2500); // Wait 2.5 seconds before spidering the next page
}
// CURRENTPAGE++;
} // end of if currentpage < length of linkqueue
} // end of function parse (data)
function start_scrape(){
LINKQUEUE.push($('#starting_url').val());
if (CURRENTPAGE<LINKQUEUE.length){
updateLinkQueueTable();
$('#currentpage').html(CURRENTPAGE);
fetchPage(LINKQUEUE[CURRENTPAGE]);
// CURRENTPAGE++;
}
// fetchPage("http://www.anandvc.com");
} // end of function start_scrape
function removelink(position_to_remove){
// add to rejected list before deleting so we don't add it again
REJECTEDLINKS.push(LINKQUEUE[position_to_remove]);
// delete it from the link queue
LINKQUEUE.splice(position_to_remove,1);
updateLinkQueueTable();
$('#currentpage').html(CURRENTPAGE);
} // end of function removelink
</script>
</head>
<body>
<h2>Find emails in pages starting from <input type="text" size=80 id="starting_url" name="starting_url" value="http://www.wpp.com/wpp/companies/company-list.htm">
<input type="submit" value="Go" name="go" id="go" onClick="return start_scrape();" onsubmit="return start_scrape();"></h2>
<h3>Number of Emails found: <span id="emailcount">0</span></h3>
<textarea id="output" style="width:500px;height:100px;" onclick="this.focus();this.select();">
</textarea>
<h3>Current Page:</h3>
<div id="currentpage">
</div>
<input type="button" id="continuebutton" name="Continue" value="Continue" label="Continue" onclick="fetchPage(LINKQUEUE[CURRENTPAGE]);" style="display:none;">
<h3>Link Queue:</h3>
<div id="linkqueue">
</div>
</body>
</html>