forked from RedFoxBot/ukrlp_1
/
scraper.php
137 lines (109 loc) · 5.21 KB
/
scraper.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
<?php
require 'scraperwiki.php';
######################################
# Basic PHP scraper
######################################
#scraperwiki::sqliteexecute("CREATE TABLE `swvariables` (`valueblob` blob, `type` text, `name` text)");
#scraperwiki::sqliteexecute("CREATE TABLE `swdata` (`datescraped` text, `primaryweb` text, `name` text, `primaryphone` text, `legalemail` text, `legalweb` text, `legalfax` text, `num` text, `trading` text, `legalphone` text, `primaryemail` text, `primaryfax` text, `primaryaddress` text, `legaladdress` text, `primarycourses` text)");
#return;
$max = 10045263;
$counter = scraperwiki::get_var('counter',10000000);
if($counter<10000000)
{
$counter=10000000;
}
for ($i=0; $i< 1000; $i++) {
$html = oneline(scraperwiki::scrape("http://www.ukrlp.co.uk/ukrlp/ukrlp_provider.page_pls_provDetails?x=&pn_p_id=".$counter."&pv_status=VERIFIED&pv_vis_code=L"));
preg_match_all('|<div class="pod_main_body">(.*?<div )class="searchleft">|',$html,$arr);
if (isset($arr[1][0])) { $code = $arr[1][0];} else { $code='';}
if ($code!='') {
#echo "code \n";
#echo json_encode($code);
#echo "\n";
preg_match_all('|<div class="provhead">UKPRN: ([0-9]*?)</div>|',$code,$num);
if (isset($num [1][0])) { $num = trim($num [1][0]);} else { $num ='';}
#echo "num \n";
#echo json_encode($num);
#echo "\n";
preg_match_all('|<div class="pt">(.*?)<|',$code,$name);
if (isset($name [1][0])) { $name = trim($name [1][0]);} else { $name ='';}
#echo "name \n";
#echo json_encode($name);
#echo "\n";
preg_match_all('|<div class="tradingname">Trading Name: <span>(.*?)</span></div>|',$code,$trading);
if (isset($trading[1][0])) { $trading = trim($trading[1][0]);} else { $trading='';}
#echo "trading \n";
#echo json_encode($trading);
#echo "\n";
preg_match_all('|<div class="assoc">Legal Address</div>(.*?)<div|',$code,$legal);
if (isset($legal [1][0])) { $legal = trim($legal [1][0]);} else { $legal ='';}
#echo "legal \n";
#echo json_encode($legal);
#echo "\n";
preg_match_all('|<div class="assoc">Primary contact address</div>(.*?)<div|',$code,$primary);
if (isset($primary[1][0])) { $primary= trim($primary[1][0]);} else { $primary='';}
#echo "primary \n";
#echo json_encode($primary);
#echo "\n";
$primary = parseAddress($primary);
$legal= parseAddress($legal);
if (trim($name)!='') {
scraperwiki::save_sqlite(
array('ukprn'),
array(
'ukprn' => clean($num),
'instname' => clean($name),
'trading' => clean($trading),
),
"data"
);
}
scraperwiki::save_var('counter',$counter);
}
$counter++;
if ($counter >= $max) {
scraperwiki::save_var('counter',10000000);
$i= 1001;
}
}
function parseAddress($val) {
preg_match_all('|<strong>Telephone: </strong>(.*?)<br />|',$val,$phone);
if (isset($phone[1][0])) { $dat['phone'] = trim($phone[1][0]);} else { $dat['phone']='';}
preg_match_all('|<strong>E-mail: </strong><a href="mailto:(.*?)">.*?</a><br />|',$val,$email);
if (isset($email[1][0])) { $dat['email'] = trim($email[1][0]);} else { $dat['email']='';}
preg_match_all('|<strong>Website: </strong><a target="_blank" href="(.*?)">.*?</a><br />|',$val,$web);
if (isset($web[1][0])) { $dat['web'] = trim($web[1][0]);} else { $dat['web']='';}
preg_match_all('|<strong>Fax: </strong>(.*?)<br />|',$val,$fax);
if (isset($fax[1][0])) { $dat['fax'] = trim($fax[1][0]);} else { $dat['fax']='';}
if (isset($courses[1][0])) { $dat ['courses'] = trim($courses[1][0]);} else { $dat['courses']='';}
preg_match_all('|<strong>Courses: </strong>(.*?)<br />|',$val,$courses);
$p = explode('<strong>',$val);
$p = explode('<br />',$p[0]);
$dat['address'] = '';
foreach ($p as $a) {
$a = trim($a);
if ($a !='') {
if ($dat['address']!='') { $dat['address'] .=', '; }
$dat['address'] .= $a;
}
}
if ($dat['address'] == 'Not specified. Please use the above.') {
$dat['address'] = '';
}
return $dat;
}
function clean($val) {
$val = str_replace(' ',' ',$val);
$val = str_replace('&','&',$val);
$val = html_entity_decode($val);
$val = strip_tags($val);
$val = trim($val);
$val = utf8_decode($val);
return($val);
}
function oneline($code) {
$code = str_replace("\n",'',$code);
$code = str_replace("\r",'',$code);
return $code;
}
?>