簡體   English   中英

將不同網頁的某些部分顯示給我

[英]Show certain part of a different webpage into mine

我希望能夠將我的服務器上排名前10的玩家從gametracker.com顯示到我的網頁中。 現在,我查找了gametracker.com頁面的源代碼,該頁面顯示了排名前10位的玩家,該部分看起來像這樣

<div class="blocknew blocknew666">
        <div class="blocknewhdr">
            TOP 10 PLAYERS <span class="item_text_12">(Online &amp; Offline)</span>
        </div>
        <table class="table_lst table_lst_stp">
            <tr>
                <td class="col_h c01">
                    Rank
                </td>
                <td class="col_h c02">
                    Name
                </td>
                <td class="col_h c03">
                    Score
                </td>
                <td class="col_h c04">
                    Time Played
                </td>
            </tr>
            .
            .
            .
            .
        </table>
        <div class="item_h10">
        </div>
<a class="fbutton" href="/server_info/*.*.*.*:27015/top_players/">
            View All Players &amp; Stats
        </a>
    </div>

如您所見,我想要的內容在class="blocknew blocknew666"之內,如果它在id中,我可以很容易地將其取出,但是當內容在一個類之內時,我不知道如何處理它。 我在互聯網上抬頭看了一眼

// Create DOM from URL or file
$html = file_get_html('http://www.google.com/');

// Find all images
foreach($html->find('img') as $element)
       echo $element->src . '<br>';

// Find all links
foreach($html->find('a') as $element)
       echo $element->href . '<br>'; 

是否可以使用此代碼執行我想要的操作? 如果是,請編寫我需要使用的代碼行,或者給我一些有關如何解決此問題的建議。

我只發布部分答案,因為我認為這樣做可能違反GameTracker服務的使用條款,您所要求的基本上是從另一網站竊取專有內容的方法。 在執行此操作之前,您絕對應該從GameTracker獲得許可。

為此,我將使用strstr。 http://php.net/manual/zh/function.strstr.php

$html = file_get_html('http://www.gametracker.com/server_info/someip/');
$topten = strstr($html, 'TOP 10 PLAYERS');
echo $topten; //this will print everthing after the content you looked for.

現在,我將由您自己決定如何截取排名前十的內容,以及如何獲得GameTracker的許可以使用它。

根據震顫的建議,這是上述問題的有效代碼

    <?php

function rstrstr($haystack,$needle)
    {
        return substr($haystack, 0,strpos($haystack, $needle));
    }

$html = file_get_contents('http://www.gametracker.com/server_info/*.*.*.*:27015/');
$topten = strstr($html, 'TOP 10 PLAYERS');//this will print everthing after the content you looked for.
$topten = strstr($topten, '<table class="table_lst table_lst_stp">');
$topten = rstrstr($topten,'<div class="item_h10">'); //this will trim stuff that is not needed
echo $topten;

?>

您提供的代碼是simpledom庫的一部分

http://simplehtmldom.sourceforge.net/

您需要下載並包含該庫才能使代碼正常工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM