Skip to content Skip to sidebar Skip to footer

Html Parsing - Get Data From A Table Inside A Div?

I am relatively new to the whole idea for HTML parsing/scraping. I was hoping that I could come here to get the help that I need! Basically what I am looking to do (i think), is sp

Solution 1:

I think I got it to work, and I learned a lot! :)

<?php//Get the current timestamp$url = 'http://www.epgpweb.com/api/snapshot/us/Caelestrasz/Crimson';
$url = file_get_contents($url);
$url = substr($url,-12,10); 

//Get the member data based on the timestamp$url = 'http://www.epgpweb.com/api/snapshot/us/Caelestrasz/Crimson/'.$url;
$url = file_get_contents($url);

//Convert the unicode to html entities, as I found here: http://stackoverflow.com/questions/2934563/how-to-decode-unicode-escape-sequences-like-u00ed-to-proper-utf-8-encoded-charfunctionreplace_unicode_escape_sequence($match) {
    return mb_convert_encoding(pack('H*', $match[1]), 'UTF-8', 'UCS-2BE');
}
$url = preg_replace_callback('/\\\\u([0-9a-f]{4})/i', 'replace_unicode_escape_sequence', $url);

//erase/replace the insignificant parts, to put the data into an arrayfunctionerase($a){
    global$url;
    $url = explode($a,$url);
    $url = implode("",$url);
}
functionreplace($a,$b){
    global$url;
    $url = explode($a,$url);
    $url = implode($b,$url);    
}
replace("[[",";");
replace("]]",";");
replace("],",";");
erase('[');
erase('"');
replace(":",",");
$url = explode(";", $url);

//lose the front and end bits, and maintain the member data
array_shift($url);
array_pop($url);

//put the data into an arrayforeach($urlas$k=>$v){
    $v = explode(",",$v);
    foreach($vas$k2=>$v2){
        $data[$k][$k2] = $v2;
    }
    $pr = round(intval($data[$k][1]) / intval($data[$k][2]),3);
    $pr = str_pad($pr,5,"0",STR_PAD_RIGHT);
    $pr = substr($pr, 0, 5);
    $data[$k][3] = $pr;
}

//sort the array by PR numberfunctioncompare($x, $y)
{
if ( $x[3] == $y[3] )
 return0;
elseif ( $x[3] > $y[3] )
 return -1;
elsereturn1;
}
usort($data, 'compare');

//output the data into a tableecho"<table><tbody><tr><th>Member</th><th>EP</th><th>GP</th><th>PR</th></tr>";
foreach($dataas$k=>$v){
    echo"<tr>";
    foreach($vas$v2){ 
        echo"<td>".$v2."</td>";
    }
    echo"</tr>";
}
echo"</tbody></table>";
?>

Solution 2:

Take a look at the PHP simple_html_dom class.

Next this will do the trick.

$html = file_get_html('http://www.epgpweb.com/guild/us/Caelestrasz/Crimson/');
$table = $html->find('#snapshot_table table.listing');

Post a Comment for "Html Parsing - Get Data From A Table Inside A Div?"