Php 获取站点的价值
因此,正如标题所说,我希望获得该网站的价值: 我的服务器名为Zath Co,现在我们排名第11。 我想要的是,一个脚本将告诉我我们的排名,我们有多少进退。唯一的问题是我们在列表中的位置上下浮动,所以我想要一个脚本来检查名字,而不是排名,但我不能从中走出来。 我试过这个剧本Php 获取站点的价值,php,Php,因此,正如标题所说,我希望获得该网站的价值: 我的服务器名为Zath Co,现在我们排名第11。 我想要的是,一个脚本将告诉我我们的排名,我们有多少进退。唯一的问题是我们在列表中的位置上下浮动,所以我想要一个脚本来检查名字,而不是排名,但我不能从中走出来。 我试过这个剧本 <?php $lines = file('http://xtremetop100.com/conquer-online'); while ($line = array_shift($lines)) { if
<?php $lines = file('http://xtremetop100.com/conquer-online');
while ($line = array_shift($lines)) {
if (strpos($line, 'Zath-Co') !== false) break; }
print_r(explode(" ", $line)); ?>
但它只显示我的服务器的名称和描述。
我怎样才能让它按照我的意愿工作,或者我必须使用真正不同的东西。如果是,那么使用什么,举个例子就好了。我建议使用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();
它也可以通过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中取出每个元素,您可以自己修复布局。谢谢您的帮助。但是我怎么能让它也显示出进进出出呢?谢谢你的帮助。这正是我想要的。我在哪里可以联系你?没问题,我很高兴你能用它来做你的事情。如有进一步问题,请通过邮件与我联系-hskrijelj@gmail.com