开发者

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 specify the url of the page I wish to grab the data from. In this case - http://www.epgpweb.com/guild/us/Caelestrasz/Crimson/

From there, I want to grab the table class=listing in the div id=snapshot_table.

I then wish to embed that table onto my own page and have it update when the original content is updated.

I have read a few of the other posts on Google and Stackoverflow, I also had a 开发者_如何学编程look at a tutorial on Nettuts+ but it just seemed to be a bit too much to take in at once.

Hopefully someone here can help me out and make this as simple as possible :)

Cheers,

Mat

--Edit--

Current code as of 11:22am (GMT+10)

<?php
    # don't forget the library
    include('simple_html_dom.php');
?>
<html>
</head>
<body>
<?php
    $html = file_get_html('http://www.epgpweb.com/guild/us/Caelestrasz/Crimson/');
    $table = $html->find('#snapshot_table table.listing');
    print_r($table);
?>
</body>
</html>


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-char
function replace_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 array
function erase($a){
    global $url;
    $url = explode($a,$url);
    $url = implode("",$url);
}
function replace($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 array
foreach($url as $k=>$v){
    $v = explode(",",$v);
    foreach($v as $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 number
function compare($x, $y)
{
if ( $x[3] == $y[3] )
 return 0;
else if ( $x[3] > $y[3] )
 return -1;
else
 return 1;
}
usort($data, 'compare');

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


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');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜