2016-06-15 41 views
0

我想获得给定网站中的所有超链接。所以我写这个代码。但其工作不正常。它只显示给定网址的全部超链接。但我想获得给定网站的所有超链接。得到给定网址的整个页面的所有href

<?php 
function getAlllinks($site){ 
$link = file_get_contents($site); 
$dom = new DOMDocument; 
@$dom->loadHTML($link); 
$links = $dom->getElementsByTagName('a'); 

foreach ($links as $link){ 

    $url = $link->getAttribute('href'); 

if($url[0]!="#" && $url[0]!=" "){ 

    echo $url. '<br>'; 
    getAlllinks($url); 

    } 

} 

}getAlllinks("http://www.example.com"); 
?> 

例如http://www.example.com

<html> 
<body> 
    <a href="index.php">Homepage</a> 
    <a href="contact.php">Contact</a> 
</body> 
</html> 

这里首先会显示超级链接的index.php和contact.php &然后将显示的index.php和contact.php或接触的所有链接。 php可以是http://www.example.com/contact.php

+0

搜索如何抓取使用PHP网站 – SML

回答

相关问题