Разбор HTML - Получить данные из таблицы внутри div? - PullRequest
3 голосов
/ 25 июля 2011

Я относительно новичок во всей идее разбора / анализа HTML-кода.Я надеялся, что смогу прийти сюда, чтобы получить помощь, которая мне нужна!

В основном то, что я хочу сделать (я думаю), это указать URL страницы, с которой я хочу получить данные.В этом случае - http://www.epgpweb.com/guild/us/Caelestrasz/Crimson/

Оттуда я хочу получить таблицу class = list в div id = snapshot_table.

Затем я хочу внедрить эту таблицу на мою собственную страницу иобновлять его при обновлении исходного содержимого.

Я прочитал несколько других постов в Google и Stackoverflow, я также посмотрел учебник по Nettuts +, но он показался мне слишком сложным.принять сразу.

Надеюсь, кто-то здесь может помочь мне и сделать это как можно проще:)

Ура,

Мат

--Edit -

Текущий код по состоянию на 11:22 (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>

Ответы [ 2 ]

3 голосов
/ 25 июля 2011

Я думаю, что получил его на работу, и я многому научился! :)

<?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: /1715980/kak-dekodirovat-escape-posledovatelnosti-unicode-takie-kak-u00ed-v-sootvetstvuyschie-simvoly-v-kodirovke-utf-8
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>";
?>
1 голос
/ 25 июля 2011

Взгляните на PHP simple_html_dom класс .

Далее это поможет.

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