2015-04-03 37 views
0

我想湊這個網址XPath的出現誤導輸出簡單的HTML DOM

http://www.gosugamers.net/counterstrike/news/archive

我使用XPath的幫助來創建以下路徑每一行:

//div[class='content']/table[@class='simple gamelist medium']/tbody/tr 

這應該打印tbody中的每一行,但是當我在簡單的html dom中嘗試此操作時,它會返回帶有標題,日期和註釋的thead。它如何不像xpath助手那樣返回tbody呢?

include('simple_html_dom.php'); 



    function getHTML($url,$timeout) 
{ 
     $ch = curl_init($url); // initialize curl with given url 
     curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]); // set useragent 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // write the response to a variable 
     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // follow redirects if any 
     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); // max. seconds to execute 
     curl_setopt($ch, CURLOPT_FAILONERROR, 1); // stop when it encounters an error 
     return @curl_exec($ch); 
} 

    $html = str_get_html(getHTML("http://www.gosugamers.net/counterstrike/news/archive",10)); 



    $table = $html->find("//div[class='content']/table[@class='simple gamelist medium']/tbody/tr",0); 

    echo $table; 

回答

0

更新

的simplehtmldom庫似乎並不支持位置謂詞的XPath。要獲得特定的行,您需要將基於0的索引作爲第二個參數傳遞給find()

爲了得到第一個非標題行(第二個表列):

$table = $html->find("//div[class='content']/table[@class='simple gamelist medium']/tbody/tr", 1); 

working phpfiddle

您XPath表達式選擇每個`tr`元件。如果您想要整個`tbody`元素,請從表達式末尾刪除`/ tr`。 如果您只需要表格單元格(`td`),請添加`/ td [1]`。 如果您只想要標題,請添加`/ td [1]/a/string()`。
+0

好的,但我如何獲得第二排?我嘗試過[2],但似乎沒有工作 – 2015-04-03 18:10:41

+0

你從//[div class ='content']/table [@ class ='simple gamelist medium']/tbody/tr [2] '?沒有結果? – joemfb 2015-04-03 18:11:59

+0

沒有結果!奇怪的是 – 2015-04-03 18:14:47