2016-04-29 80 views
1

我想列出頁面上的所有鏈接和名稱。我不斷收到銀行的輸出低於返回xpath的空白輸出

$url="http://www.ciim.in/top-pr-dofollow-social-bookmarking-sites-list-2016"; 
$html = file_get_contents($url); 

代碼和節點部分

$nodes = $my_xpath->query('//table[@class="social_list"]/tbody/tr'); 

    foreach($nodes as $node) 
    { 

    $title = $my_xpath->evaluate('td[1]/a"]', $node); 
    $link = $my_xpath->evaluate('td[1]/a/@href"]', $node); 

    echo $title.",".$link."<br>";   

    } 

筆記右鍵點擊該網站上被禁用,我使用的開發工具來檢查代碼中鉻元素

回答

2

查詢

$nodes = $xpath->query('//table[@class="social_list"]/tbody/tr/td/a'); 

裏面的foreach更有效地獲得標題和URL

$title = $node->textContent; 
$href = $node->getAttribute('href'); 

編輯: 我已經測試此代碼檢索整個表

//Query from parent div 
$nodes = $xpath->query('//div[@class="table_in_overflow"]'); 

foreach ($nodes as $node) { 
    $a = $node->getElementsByTagName("a"); 
    foreach($a as $item) { 
     $href = $item->getAttribute("href"); 
     $text = $item->nodeValue; 
    } 
} 
+0

不錯的見解! +1 – splash58

+0

工作...真棒! –

1

您在您的選擇'td[1]/a"]''td[1]/a/@href"]'結束後"],所以更改這些簡單是td[1]/atd[1]/a/@href

此外,您可以通過選擇僅trtda來改善您的xpath,因此這將忽略沒有鏈接的標頭。

'//table[@class="social_list"]/tbody/tr[td/a]' 

,這將是比'//table[@class="social_list"]/tbody/tr'

+1

這已經是在代碼中 –

+1

沒有在你的代碼是'']'廣告結束。我不明白這應該是什麼。但是,你只有空白輸出還是隻有第一行? –