Source Code



Parse From Detik News for PEMILU 2014 Indonesia Quick Count

  • anonymous
  • Jul 09' 14
  • PHP
  • 315
  • 77
  • United States

Hi, this can use for parse data from detik.com about PILRES 2014, in this case no changes data structure.

<?php
$presiden =pemilu('http://news.detik.com/pemilu2014');

function pemilu($pk){
$html = file_get_contents($pk);
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML($html); 
$xpath = new DOMXPath($doc);
$nodelist = $xpath->query("//span"); 


$i=0;
$data=array();
foreach($nodelist as $key){
	$node = $nodelist->item($i); 
	$value = isset($node->attributes->getNamedItem('class')->nodeValue)?$node->attributes->getNamedItem('class')->nodeValue:'';
    if($value=='color_bar'){
	//echo $node->attributes->getNamedItem('style')->nodeValue;
	$int = filter_var($node->attributes->getNamedItem('style')->nodeValue, FILTER_SANITIZE_NUMBER_INT);
	$int = substr($int,0,2).','.substr($int,-2).'%';
	$data[]=array('hasil'=>$node->attributes->getNamedItem('style')->nodeValue,'real'=>$int);
	}
    
	$i++;
}
return $data;
}
print_r (json_encode($presiden)); 
?>


This for result :

[{"hasil":"width: 47.41%;","real":"47,41%"},{"hasil":"width: 52.59%;","real":"52,59%"}]

....

  • Tags : news, detik, pemilu
comments powered by Disqus



Category

Tag Linked