簡體   English   中英

獲取給定網址的整個頁面的所有href

[英]get all a href of entire page of given url

我想在給定站點中獲取所有超鏈接。 所以我寫這段代碼。 但它無法正常工作。 它僅顯示給定URL的所有超鏈接。 但我想獲取給定站點的所有超鏈接。

<?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的所有鏈接,或者contact.php可以是http://www.example.com/contact.php

我認為您要嘗試做的是抓取整個網站,收集所有鏈接。 您的代碼示例無法做到這一點。 您要執行的操作將加載每個頁面,抓住每個頁面上的每個鏈接,然后遞歸這些鏈接。

查看這些鏈接以獲取更多信息:

如何使用PHP創建簡單的搜尋器?

https://zh.wikipedia.org/wiki/Web_crawler http://phpcrawl.cuab.de/example.html

暫無
暫無

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

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