获得网站的价值

时间:2012-04-07 11:22:53

标签: php

因为标题说我希望得到这个网站的价值:Xtremetop100 Conquer-Online

我的服务器名为Zath-Co,现在我们的排名是11。 我想要的是一个剧本会告诉我我们的排名,我们的排名和排名。唯一的问题是我们在列表中上下起伏所以我想要一个脚本来检查不在等级的名称,但我不能从中得出结论。 我试过这个剧本

  <?php $lines = file('http://xtremetop100.com/conquer-online');
  while ($line = array_shift($lines)) {
  if (strpos($line, 'Zath-Co') !== false) break; }
  print_r(explode(" ", $line)); ?>

但它只显示我的服务器名称和描述。 我怎样才能让这个按照我的意愿去做,或者我必须使用一些非常不同的东西。 (如果是,那么使用什么,一个例子会很棒。)

2 个答案:

答案 0 :(得分:0)

我建议使用SimpleXML和XPath。这是一个有效的例子:

$html = file_get_contents('http://xtremetop100.com/conquer-online');

// suppress invalid markup warnings
libxml_use_internal_errors(true);

// Create SimpleXML object
$doc = new DOMDocument();
$doc->strictErrorChecking = false;
$doc->loadHTML($html);
$xml = simplexml_import_dom($doc);

$xpath = '//span[@class="hd1" and ./a[contains(., "Zath-Co")]]/ancestor::tr/td[@class="number" or @class="stats1" or @class="stats"]';
$anchor = $xml->xpath($xpath);

// Clear invalid markup error buffer
libxml_clear_errors();

$rank = (string)$anchor[0]->b;
$in   = (string)$anchor[1]->span;
$out  = (string)$anchor[2]->span;

// Clear invalid markup error buffer
libxml_clear_errors();

答案 1 :(得分:0)

也可以使用file() - 函数修复它,就像你自己尝试一样。您只需要查找源代码并找到“部分”的起始行。我发现(在源代码中),你需要7行来获得排名,描述和输入/输出数据。这是一个经过测试的例子:

<?php 

$lines = file('http://xtremetop100.com/conquer-online');
$CountLines = sizeof( $lines);
$arrHtml = array();
for( $i = 0; $i < $CountLines; $i++) {
    if( strpos( $lines[$i], '/sitedetails-1132314895')) {
        //The seven lines taken here under is your section at the site
        $arrHtml[] = $lines[$i++];
        $arrHtml[] = $lines[$i++]; 
        $arrHtml[] = $lines[$i++];
        $arrHtml[] = $lines[$i++];
        $arrHtml[] = $lines[$i++];
        $arrHtml[] = $lines[$i++];
        $arrHtml[] = $lines[$i++];
        break;
    }
}

//We simply strip all tags, so you just got the content.
$arrHtml = array_map("strip_tags", $arrHtml);

//Here we echo the data
echo implode('<br>',$arrHtml);

?>

您可以通过从循环中取出$ arrHtml中的每个元素来自行修复布局。