-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrape.php
801 lines (670 loc) · 18.8 KB
/
scrape.php
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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
<?php
error_reporting(E_ALL ^ E_NOTICE);
//phpinfo(); exit;
/**
* Curl based HTTP Client
* Simple but effective OOP wrapper around Curl php lib.
* Contains common methods needed
* for getting data from url, setting referrer, credentials,
* sending post data, managing cookies, etc.
*
* Samle usage:
* $curl = &new Curl_HTTP_Client();
* $useragent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
* $curl->set_user_agent($useragent);
* $curl->store_cookies("/tmp/cookies.txt");
* $post_data = array('login' => 'pera', 'password' => 'joe');
* $html_data = $curl->send_post_data(http://www.foo.com/login.php, $post_data);
*/
class Curl_HTTP_Client
{
/**
* Curl handler
* @access private
* @var resource
*/
var $ch ;
/**
* set debug to true in order to get usefull output
* @access private
* @var string
*/
var $debug = false;
/**
* Contain last error message if error occured
* @access private
* @var string
*/
var $error_msg;
/**
* Curl_HTTP_Client constructor
* @param boolean debug
* @access public
*/
function Curl_HTTP_Client($debug = false)
{
$this->debug = $debug;
$this->init();
}
/**
* Init Curl session
* @access public
*/
function init()
{
// initialize curl handle
$this->ch = curl_init();
//set various options
//set error in case http return code bigger than 300
curl_setopt($this->ch, CURLOPT_FAILONERROR, true);
// allow redirects
curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, true);
// use gzip if possible
curl_setopt($this->ch,CURLOPT_ENCODING , 'gzip, deflate');
// do not veryfy ssl
// this is important for windows
// as well for being able to access pages with non valid cert
curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, 0);
}
/**
* Set username/pass for basic http auth
* @param string user
* @param string pass
* @access public
*/
function set_credentials($username,$password)
{
curl_setopt($this->ch, CURLOPT_USERPWD, "$username:$password");
}
/**
* Set referrer
* @param string referrer url
* @access public
*/
function set_referrer($referrer_url)
{
curl_setopt($this->ch, CURLOPT_REFERER, $referrer_url);
}
/**
* Set client's useragent
* @param string user agent
* @access public
*/
function set_user_agent($useragent)
{
curl_setopt($this->ch, CURLOPT_USERAGENT, $useragent);
}
/**
* Set to receive output headers in all output functions
* @param boolean true to include all response headers with output, false otherwise
* @access public
*/
function include_response_headers($value)
{
curl_setopt($this->ch, CURLOPT_HEADER, $value);
}
function include_response_headers_only()
{
curl_setopt($this->ch, CURLOPT_HEADER, true);
curl_setopt($this->ch, CURLOPT_NOBODY, true);
curl_setopt($this->ch, CURLOPT_MAXREDIRS, 1);
}
/**
* Set proxy to use for each curl request
* @param string proxy
* @access public
*/
function set_proxy($proxy)
{
curl_setopt($this->ch, CURLOPT_PROXY, $proxy);
}
function set_headers($headers)
{
curl_setopt($this->ch, CURLOPT_HTTPHEADER, $headers);
}
/**
* Send post data to target URL
* return data returned from url or false if error occured
* @param string url
* @param mixed post data (assoc array ie. $foo['post_var_name'] = $value or as string like var=val1&var2=val2)
* @param string ip address to bind (default null)
* @param int timeout in sec for complete curl operation (default 10)
* @return string data
* @access public
*/
function send_post_data($url, $postdata, $ip=null, $timeout=10)
{
//set various curl options first
// set url to post to
curl_setopt($this->ch, CURLOPT_URL,$url);
// return into a variable rather than displaying it
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER,true);
//bind to specific ip address if it is sent trough arguments
if($ip)
{
if($this->debug)
{
echo "Binding to ip $ip\n";
}
curl_setopt($this->ch,CURLOPT_INTERFACE,$ip);
}
//set curl function timeout to $timeout
curl_setopt($this->ch, CURLOPT_TIMEOUT, $timeout);
//set method to post
curl_setopt($this->ch, CURLOPT_POST, true);
//generate post string
$post_array = array();
if(is_array($postdata))
{
foreach($postdata as $key=>$value)
{
$post_array[] = urlencode($key) . "=" . urlencode($value);
}
$post_string = implode("&",$post_array);
if($this->debug)
{
echo "Url: $url\nPost String: $post_string\n";
}
}
else
{
$post_string = $postdata;
}
// set post string
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $post_string);
//and finally send curl request
$result = curl_exec($this->ch);
if(curl_errno($this->ch))
{
if($this->debug)
{
echo "Error Occured in Curl\n";
echo "Error number: " .curl_errno($this->ch) ."\n";
echo "Error message: " .curl_error($this->ch)."\n";
}
return false;
}
else
{
return $result;
}
}
/**
* fetch data from target URL
* return data returned from url or false if error occured
* @param string url
* @param string ip address to bind (default null)
* @param int timeout in sec for complete curl operation (default 5)
* @return string data
* @access public
*/
function fetch_url($url, $ip=null, $timeout=5)
{
// set url to post to
curl_setopt($this->ch, CURLOPT_URL,$url);
//set method to get
curl_setopt($this->ch, CURLOPT_HTTPGET,true);
// return into a variable rather than displaying it
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER,true);
//bind to specific ip address if it is sent trough arguments
if($ip)
{
if($this->debug)
{
echo "Binding to ip $ip\n";
}
curl_setopt($this->ch,CURLOPT_INTERFACE,$ip);
}
//set curl function timeout to $timeout
curl_setopt($this->ch, CURLOPT_TIMEOUT, $timeout);
//and finally send curl request
$result = curl_exec($this->ch);
if(curl_errno($this->ch))
{
if($this->debug)
{
echo "Error Occured in Curl\n";
echo "Error number: " .curl_errno($this->ch) ."\n";
echo "Error message: " .curl_error($this->ch)."\n";
}
return false;
}
else
{
return $result;
}
}
/**
* Fetch data from target URL
* and store it directly to file
* @param string url
* @param resource value stream resource(ie. fopen)
* @param string ip address to bind (default null)
* @param int timeout in sec for complete curl operation (default 5)
* @return boolean true on success false othervise
* @access public
*/
function fetch_into_file($url, $fp, $ip=null, $timeout=5)
{
// set url to post to
curl_setopt($this->ch, CURLOPT_URL,$url);
//set method to get
curl_setopt($this->ch, CURLOPT_HTTPGET, true);
// store data into file rather than displaying it
curl_setopt($this->ch, CURLOPT_FILE, $fp);
//bind to specific ip address if it is sent trough arguments
if($ip)
{
if($this->debug)
{
echo "Binding to ip $ip\n";
}
curl_setopt($this->ch, CURLOPT_INTERFACE, $ip);
}
//set curl function timeout to $timeout
curl_setopt($this->ch, CURLOPT_TIMEOUT, $timeout);
//and finally send curl request
$result = curl_exec($this->ch);
if(curl_errno($this->ch))
{
if($this->debug)
{
echo "Error Occured in Curl\n";
echo "Error number: " .curl_errno($this->ch) ."\n";
echo "Error message: " .curl_error($this->ch)."\n";
}
return false;
}
else
{
return true;
}
}
/**
* Send multipart post data to the target URL
* return data returned from url or false if error occured
* (contribution by vule nikolic, vule@dinke.net)
* @param string url
* @param array assoc post data array ie. $foo['post_var_name'] = $value
* @param array assoc $file_field_array, contains file_field name = value - path pairs
* @param string ip address to bind (default null)
* @param int timeout in sec for complete curl operation (default 30 sec)
* @return string data
* @access public
*/
function send_multipart_post_data($url, $postdata, $file_field_array=array(), $ip=null, $timeout=30)
{
//set various curl options first
// set url to post to
curl_setopt($this->ch, CURLOPT_URL, $url);
// return into a variable rather than displaying it
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
//bind to specific ip address if it is sent trough arguments
if($ip)
{
if($this->debug)
{
echo "Binding to ip $ip\n";
}
curl_setopt($this->ch,CURLOPT_INTERFACE,$ip);
}
//set curl function timeout to $timeout
curl_setopt($this->ch, CURLOPT_TIMEOUT, $timeout);
//set method to post
curl_setopt($this->ch, CURLOPT_POST, true);
// disable Expect header
// hack to make it working
$headers = array("Expect: ");
curl_setopt($this->ch, CURLOPT_HTTPHEADER, $headers);
// initialize result post array
$result_post = array();
//generate post string
$post_array = array();
$post_string_array = array();
if(!is_array($postdata))
{
return false;
}
foreach($postdata as $key=>$value)
{
$post_array[$key] = $value;
$post_string_array[] = urlencode($key)."=".urlencode($value);
}
$post_string = implode("&",$post_string_array);
if($this->debug)
{
echo "Post String: $post_string\n";
}
// set post string
//curl_setopt($this->ch, CURLOPT_POSTFIELDS, $post_string);
// set multipart form data - file array field-value pairs
if(!empty($file_field_array))
{
foreach($file_field_array as $var_name => $var_value)
{
if(strpos(PHP_OS, "WIN") !== false) $var_value = str_replace("/", "\\", $var_value); // win hack
$file_field_array[$var_name] = "@".$var_value;
}
}
// set post data
$result_post = array_merge($post_array, $file_field_array);
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $result_post);
//and finally send curl request
$result = curl_exec($this->ch);
if(curl_errno($this->ch))
{
if($this->debug)
{
echo "Error Occured in Curl\n";
echo "Error number: " .curl_errno($this->ch) ."\n";
echo "Error message: " .curl_error($this->ch)."\n";
}
return false;
}
else
{
return $result;
}
}
/**
* Set file location where cookie data will be stored and send on each new request
* @param string absolute path to cookie file (must be in writable dir)
* @access public
*/
function store_cookies($cookie_file)
{
// use cookies on each request (cookies stored in $cookie_file)
curl_setopt ($this->ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt ($this->ch, CURLOPT_COOKIEFILE, $cookie_file);
}
/**
* Set custom cookie
* @param string cookie
* @access public
*/
function set_cookie($cookie)
{
curl_setopt ($this->ch, CURLOPT_COOKIE, $cookie);
}
/**
* Get last URL info
* usefull when original url was redirected to other location
* @access public
* @return string url
*/
function get_effective_url()
{
return curl_getinfo($this->ch, CURLINFO_EFFECTIVE_URL);
}
/**
* Get http response code
* @access public
* @return int
*/
function get_http_response_code()
{
return curl_getinfo($this->ch, CURLINFO_HTTP_CODE);
}
/**
* Return last error message and error number
* @return string error msg
* @access public
*/
function get_error_msg()
{
$err = "Error number: " .curl_errno($this->ch) ."\n";
$err .="Error message: " .curl_error($this->ch)."\n";
return $err;
}
/**
* Close curl session and free resource
* Usually no need to call this function directly
* in case you do you have to call init() to recreate curl
* @access public
*/
function close()
{
//close curl session and free up resources
curl_close($this->ch);
}
}
// EXAMPLE...
/**
* @version $Id$
* @package dinke.net
* @copyright © 2005 Dinke.net
* @author Dragan Dinic <dragan@dinke.net>
*/
/*
require_once("curl_client.php");
$curl = &new Curl_HTTP_Client();
//pretend to be IE6 on windows
$useragent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
$curl->set_user_agent($useragent);
//uncomment next two lines if you want to manage cookies
//$cookies_file = "/tmp/cookies.txt";
//$curl->store_cookies($cookies_file);
//Uncomment next line if you want to set credentials
//$curl->set_credentials($username, $password);
//Uncomment next line if you want to set specific referrer
//$curl->set_referrer("http://my.referrer.url");
//if you want to send some post data
//form post data array like this one
$post_data = array('login' => 'pera', 'password' => 'joe', 'other_foo_field' => 'foo_value');
//and send request to http://www.foo.com/login.php. Result page is stored in $html_data string
$html_data = $curl->send_post_data("http://www.foo.com/login.php", $post_data);
//You can also fetch data from somewhere using get method!
//Fetch html from url
$html_data = $curl->fetch_url("http://www.foo.com/foobar.php?login=pera&password=joe&other_foo_field=foo_value");
//if you have more than one IP on your server,
//you can also bind to specific IP address like ...
//$bind_ip = "192.168.0.1";
//$curl->fetch_url("http://www.foo.com/login.php", $bind_ip);
//$html_data = $curl->send_post_data("http://www.foo.com/login.php", $post_data, $bind_ip);
*/
function curl($url,$referrer=0,$post=0,$cookies=0,$auth=0,$headersOnly=0,$debug=0){
$curl = new Curl_HTTP_Client();
if ($debug) $curl->debug = 0;
//$url = 'http://web-sniffer.net/?url='.urlencode($url).'&rawhtml=yes&submit=Submit&http=1.1&gzip=yes&type=GET&uak=1';
//print '<br><br>URL:'.$url.'<br>';
//pretend to be IE6 on windows
$useragent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
$curl->set_user_agent($useragent);
//uncomment next two lines if you want to manage cookies
if ($cookies!=0){
$cookies_file = "cookies/cookies.txt";
$curl->store_cookies($cookies_file);
}
//Uncomment next line if you want to set credentials
if ($auth!=0){
$username = $auth[0];
$password = $auth[1];
$curl->set_credentials($username, $password);
}
if ($headersOnly){
$curl->include_response_headers_only();
}
//Uncomment next line if you want to set specific referrer
if ($referrer!=0){
$curl->set_referrer($referrer);
$referrerStr='Referer: '.$str;
} else {
//$referrer='Referer: ';
$referrerStr='';
}
$host = explode('/',$url);
$host = $host[2];
//Set Headers
$headers=array('GET / HTTP/1.1','Host: '.$host,'Connection: close','User-Agent: '.$useragent,'Accept-Encoding: gzip','Accept-Charset: ISO-8859-1,UTF-8;q=0.7,*;q=0.7','Cache-Control: no','Accept-Language: de,en;q=0.7,en-us;q=0.3',$referrerStr);
$curl->set_headers($headers);
//if you want to send some post data
//form post data array like this one
if ($post!=0){
$post_data = $post; // $post = array('login' => 'pera', 'password' => 'joe', 'other_foo_field' => 'foo_value');
//and send request to http://www.foo.com/login.php. Result page is stored in $html_data string
$html_data = $curl->send_post_data($url, $post_data);
} else {
$html_data = $curl->fetch_url($url);
}
//You can also fetch data from somewhere using get method!
//Fetch html from url
//$html_data = $curl->fetch_url("http://www.foo.com/foobar.php?login=pera&password=joe&other_foo_field=foo_value");
//if you have more than one IP on your server,
//you can also bind to specific IP address like ...
//$bind_ip = "192.168.0.1";
//$curl->fetch_url("http://www.foo.com/login.php", $bind_ip);
//$html_data = $curl->send_post_data("http://www.foo.com/login.php", $post_data, $bind_ip);
$curl->close();
if ($curl->debug || $_REQUEST['debug']==1) {
print '<textarea>'.$html_data.'</textarea>'; //exit;
}
return $html_data;
}
function parseData($startString,$endString,$data,$stripTags='0'){
//print $line;
$parsedData=explode(html_entity_decode($startString),html_entity_decode($data),2);
$parsedData=explode(html_entity_decode($endString),$parsedData[1],2);
if ($stripTags) return trim(strip_tags($parsedData[0]));
return trim($parsedData[0]);
}
function parseXML($element,$data){
$startString='<'.$element.'>';
$endString='</'.$element.'>';
$parsedData=explode($startString,$data);
$parsedData=explode($endString,$parsedData[1]);
return $parsedData[0];
}
// collectData: Parses Data and Explodes it into an array, which is returned
function collectData($pre,$post,$split,$data){
$list = parseData($pre,$post,$data);
$list = explode($split,$list);
return $list;
}
// returns array(link url,contents between <a> and </a>)
function getLink($input) {
$regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>";
if(preg_match_all("/$regexp/siU", $input, $matches)) {
# $matches[2] = array of link addresses
# $matches[3] = array of link text - including HTML code
return(array($matches[2][0],$matches[3][0]));
}
}
// scrape logic
$cities = Array("Alameda",
"Albany",
"Berkeley",
"Castro Valley",
"Dublin",
"Emeryville",
"Fremont",
"Hayward",
"Livermore",
"Newark",
"Oakland",
"Pleasanton",
"San Leandro",
"San Lorenzo",
"Union City",
"Antioch",
"Brentwood",
"Concord",
"Danville",
"El Cerrito",
"El Sobrante",
"Hercules",
"Lafayette",
"Martinez",
"Pinole",
"Pittsburg",
"Pleasant Hill",
"San Pablo",
"San Ramon",
"Walnut Creek",
"Corte Madera",
"Larkspur",
"Mill Valley",
"Novato",
"San Rafael",
"Sausalito",
"Tiburon",
"Carmichael",
"Citrus Heights",
"Elk Grove",
"Fair Oaks",
"Folsom",
"North Highlands",
"Orangevale",
"Rancho Cordova",
"Sacramento",
"San Francisco",
"Belmont",
"Burlingame",
"Daly City",
"Menlo Park",
"Millbrae",
"Pacifica",
"Palo Alto",
"Redwood City",
"San Bruno",
"San Mateo",
"South San Francisco",
"Campbell",
"Cupertino",
"Gilroy",
"Los Altos",
"Los Gatos",
"Milpitas",
"Morgan Hill",
"Mountain View",
"Palo Alto",
"San Jose",
"Santa Clara",
"Sunnyvale",
"Benicia",
"Dixon",
"Fairfield",
"Suisun City",
"Vacaville",
"Vallejo",
"Glen Ellen",
"Petaluma",
"Rohnert Park",
"Santa Rosa",
"Sonoma");
$i = 0;
$city_data = [];
?>
<p>Result CSV:</p>
<textarea rows="20" cols="40"><?
foreach ($cities as $city) {
$city = str_replace(' ', '-', $city);
$url = 'http://www.city-data.com/city/'.$city.'-California.html';
$html = curl($url,$referrer=0,$post=0,$cookies=0,$auth=0,$headersOnly=0,$debug=0);
$hi = parseData('household income in 2012:</b> ',' (',$html);
$pci = parseData('per capita income in 2012:</b> ',' (',$html);
$population = parseData('Population in 2012:</b> ',' (',$html);
$city_data[$i] = Array();
$city_data[$i] = ["city" => $city, "pcincome" => $pci, "pop" => $population];
?><?='"'.$city.'","'.$pcincome."\"\n"?><?
$i++;
// if ($i > 1) break;
// Takes longer but helps ensure IP isn't
// black listed for too many requests
usleep(rand( 200000 , 500000)); // .2-.5 seconds per request
}
?>
</textarea>
<p>Sanity Check:</p>
<p>
<?
foreach ($city_data as $data) {
$city = str_replace(' ', '-', $data['city']);
$url = 'http://www.city-data.com/city/'.$city.'-California.html';
// $html = curl($url,$referrer=0,$post=0,$cookies=0,$auth=0,$headersOnly=0,$debug=0);
// $hi = parseData('household income in 2012:</b> ',' (',$html);
?>
<?=$data['city'].' ('.$data['pcincome'].') - <a target="_blank" href="'.$url.'">'.$url.'</a> <br>'?>
<?
}
?>
</p>