how can i extract all links from web page ?

Discussion in 'PHP' started by ramysarwat, Sep 1, 2010.

  1. #1
    how can i extract all links from web page ?
     
    ramysarwat, Sep 1, 2010 IP
  2. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #2
    Here is an example how you can do it using JavaScript. Just add this script after the tag </body>:

    
    <script type="text/javascript">
    var all_a = document.getElementsByTagName("a");
    
    var all_links = "";
    
    for(var i=0; i<all_a.length; i++){
        all_links += "\n" + all_a[i].href;
    }
    
    alert("All links:\n"+all_links);
    </script>
    
    Code (JavaScript):
     
    s_ruben, Sep 1, 2010 IP
  3. Gray Fox

    Gray Fox Well-Known Member

    Messages:
    196
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    130
    #3
    
    $url = "http://google.com";
    $domDocument = @DOMDocument::loadHTML(file_get_contents($url));
    $anchorNodeList = $domDocument->getElementsByTagName('a');
    foreach ($anchorNodeList as $a)
    	echo $a->getAttribute('href'), '<br/>';
    
    PHP:
     
    Gray Fox, Sep 1, 2010 IP