jquery - HTML 解析 - 从 div 内的表中获取数据?

标签 jquery html json parsing screen-scraping

我对 HTML 解析/抓取的整个想法比较陌生。我希望我能来这里得到我需要的帮助!

基本上我想要做的(我认为)是指定我希望从中获取数据的页面的 url。在这种情况下 - http://www.epgpweb.com/guild/us/Caelestrasz/Crimson/

从那里,我想在 div id=snapshot_table 中获取表 class=listing。

然后我希望将该表嵌入到我自己的页面上,并在原始内容更新时更新它。

我已经阅读了 Google 和 Stackoverflow 上的一些其他帖子,我还查看了 Nettuts+ 上的教程,但它似乎有点太多了,无法一次接受。

希望这里有人能帮助我,让这个尽可能简单:)

干杯,

垫子

--编辑--

截至上午 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>

最佳答案

我想我成功了,我学到了很多东西! :)

<?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>";
?>

关于jquery - HTML 解析 - 从 div 内的表中获取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6810735/

相关文章:

javascript - HTML Accordion 50/50 分屏

javascript - 如何在html div中显示pdf文件

android - JSON 中的最后一张图片未显示

javascript - 创建一个接受用户输入并在页面加载时将其添加到句子中的表单

JQuery - 如何平滑地调整图像大小以创建脉动效果?

javascript - 为什么 Twitter 小部件在 Chrome 中不显示?

android - Webview 不支持 Android Jellybean 上的 flex box

json - 解码 JSON 并区分丢失的键和空值

json - 如何在 mongodb 中展平子文档

php - 图像文件为空