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
|
<?php
set_time_limit(0);
error_reporting(0);
echo "<pre>";
$url = 'http://www.permisecole.com/annuaire-auto-ecoles/';
echo "Process Started: $url\n"; flush();
$doc = new DOMDocument('1.0', 'iso-8859-15');
$doc->strictErrorChecking = false;
if (!$doc->loadHTMLFile($url))
die("Cannot open $url");
$xpath = new DOMXPath($doc);
$departments = array();
foreach ($xpath->query('//td[@width="33%"]') as $node) {
$anchor = $node->firstChild;
if ($anchor->hasAttributes())
$departments[$anchor->nodeValue] = $anchor->getAttribute('href');
}
echo "Department List Created\n"; flush();
foreach ($departments as $department_name => $department_url) {
echo "> Processing department $department_name... "; flush();
$department_doc = new DOMDocument('1.0', 'iso-8859-15');
$department_doc->strictErrorChecking = false;
if ($department_doc->loadHTMLFile("http://www.permisecole.com" . $department_url)) {
$department_xpath = new DOMXpath($department_doc);
$towns = array();
foreach ($department_xpath->query('//td[@width="33%"]') as $node) {
$dpt_anchor = $node->firstChild;
if ($dpt_anchor->hasAttributes())
$towns[$dpt_anchor->nodeValue] = $dpt_anchor->getAttribute('href');
}
echo count($towns) . " towns found\n"; flush();
foreach ($towns as $town_name => $town_url) {
echo ">> Processing town $town_name... "; flush();
$town_doc = new DOMDocument('1.0', 'iso-8859-15');
$town_doc->strictErrorChecking = false;
if ($town_doc->loadHTMLFile("http://www.permisecole.com" . $town_url)) {
$town_xpath = new DOMXpath($town_doc);
$agencies = array();
foreach ($town_xpath->query('//td[@width="33%"]') as $node) {
$list = $node->childNodes;
$name = trim($town_xpath->query('a/b', $node)->item(0)->nodeValue);
$address = trim(($list->item(2) ? $list->item(2)->wholeText : '') . ' ' . ($list->item(4) ? $list->item(4)->wholeText : ''));
$address = str_replace(array("\r","\n","\t"), '', $address);
$tel = trim($list->item(6) ? $list->item(6)->wholeText : '');
if (!empty($tel))
$agencies[] = array('name' => $name, 'address' => $address, 'tel' => $tel);
}
echo count($agencies) . " agencies found\n"; flush();
foreach ($agencies as $a) { echo ">>> {$a['name']} - {$a['address']} - {$a['tel']}\n"; }
$towns[$town_name] = $agencies;
}
}
$departments[$department_name] = $towns;
}
}
$filename = "auto_ecoles." . date('Ymd') . '.json';
echo "Writing $filename ...";
file_put_contents($filename, json_encode($departments));
echo "--------------------------------------------------\n";
echo "DONE"; flush();
echo "<pre>"; |