Skip to content

Instantly share code, notes, and snippets.

@anunay
Created January 30, 2014 20:37
Show Gist options
  • Save anunay/8718177 to your computer and use it in GitHub Desktop.
Save anunay/8718177 to your computer and use it in GitHub Desktop.
DOM Example
<?php
include 'classes/simple_html_dom.php';
$html = file_get_html('http://www.wheel-size.com/');
$ret = $html->find('div[class=brand-icons] a');
$site_url = "http://www.wheel-size.com";
$wheel = array();
$modelelement = "";
$i = 0;
$csvdata = array();
$csvdata[] = array("Make","Year","Model","Tyre Size");
$startat = 1;
$endat= 2;
// header("Content-type: text/csv");
// header("Cache-Control: no-store, no-cache");
// header('Content-Disposition: attachment; filename="wheeldata.csv"');
echo "Make,Year,Model,Tyre Size\n\r" . "<br />";
foreach($ret as $element){
if($element->href === "#") continue;
if($i < $startat ) {
$i++;
continue;
}
if($i == $endat) break;
$wheelbrand = strip_tags($element->innertext);
$wheel[$i]["brand-name"] = $element->innertext;
$wheel[$i]["brand-url"] = $element->href;
$model_html = file_get_html($site_url. $element->href);
$model_output = $model_html->find("table[class=car-makes-list] a");
$j = 0;
$fetch = false;
foreach($model_output as $modelelement){
// /* Acura */
// if($modelelement->innertext == "TL") {
// $fetch = true;
// }
// if($fetch == false) continue;
$modelname = strip_tags($modelelement->innertext);
$year_html = file_get_html($site_url. $modelelement->href);
$year_output = $year_html->find("div[class=large-tag-view] a");
$year_array = array();
foreach($year_output as $yearelement){
$yearval = strip_tags($yearelement->innertext);
$year_array[] = array("name"=>strip_tags($yearelement->innertext), "url"=>$yearelement->href);
$tyre_html = file_get_html( $site_url. $yearelement->href );
$dr = array();
foreach($tyre_html->find("table") as $table){
$k = 0;
foreach($table->find("tr") as $tr){
if($k++ == 0 ) continue;
$c = 0;
foreach($tr->find("td") as $td){
if($c++ == 0){
$tyre_row[] = trim($td->plaintext);
echo $wheelbrand . "," . $yearval . "," . $modelname . "," . $td->plaintext . "\n\r<br />";
//$csvdata[] = array($wheelbrand, $yearval, $modelname, $td->plaintext);
//echo implode(",",$csvdata) . "\n\r";
}
}
}
}
}
$wheel[$i]["model"][] = array("name"=>strip_tags($modelelement->innertext), "url"=>$modelelement->href , "year"=>array("years"=>$year_array, "types"=>$tyre_row) );
$j++;
}
//$i++;
}
// echo "<pre>";
// print_r($wheel);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment