PHP gets all the links in the specified URL page

Source: Internet
Author: User
Tags xpath
Form:http://www.uphtm.com/php/253.html

This thing in fact, we are commonly used by developers, used to do a crawl other website friendship connection used, and today saw a friend organized a PHP get the specified URL page of all the link functions, organize us to see it together.

The following code can get all the links in the specified URL page, which is the href attribute of all the A tags:

    1. Get the HTML code for the link
    2. $html = file_get_contents (' http://www.111cn.net ');
    3. $dom = new DOMDocument ();
    4. @ $dom->loadhtml ($html);
    5. $xpath = new Domxpath ($dom);
    6. $hrefs = $xpath->evaluate ('/html/body//a ');
    7. for ($i = 0; $i < $hrefs->length; $i + +) {
    8. $href = $hrefs->item ($i);
    9. $url = $href->getattribute (' href ');
    10. echo $url. '
      ';
    11. }

This code gets all the href attributes of the a tag, but the href attribute value is not necessarily a link, we can do a filter and keep only the link address at the beginning of the http:

    1. Get the HTML code for the link
    2. $html = file_get_contents (' http://www.111cn.net ');
    3. $dom = new DOMDocument ();
    4. @ $dom->loadhtml ($html);
    5. $xpath = new Domxpath ($dom);
    6. $hrefs = $xpath->evaluate ('/html/body//a ');
    7. for ($i = 0; $i < $hrefs->length; $i + +) {
    8. $href = $hrefs->item ($i);
    9. $url = $href->getattribute (' href ');
    10. Keep links that start with HTTP
    11. if (substr ($url, 0, 4) = = = ' http ')
    12. echo $url. '
      ';
    13. }

The fopen () function reads all the links in the specified Web page and counts the number of pages, where it is appropriate to use this code, this example to read Baidu home page for example, to find out all the links in the homepage of Baidu, the code has been tested, fully available:

    1. !--?
    2. if (empty ($url)) $url = "http://www.baidu.com/";//URL address to collect the link
    3. $site =substr ($url, 0,strpos ($url, "/", 8));
    4. $base =substr ($url, 0,strrpos ($url, "/") +1);//directory where the file is located
    5. $fp = fopen ($url, "R");//Open the URL address page
    6. while (!feof ($FP)) $contents. =fread ($fp, 1024x768);
    7. $pattern = "|href=[' \"]? ([^ '\"]+) ['\" ]| U ";
    8. Preg_match_all ($pattern, $contents, $REGARR, preg_set_order);//use regular matches for all href=
    9. for ($i =0; $i
    10. if (!eregi ("://", $REGARR [$ I][1])//Determines whether it is a relative path, that is, whether there is://
    11. if (substr ($regArr [$i][1],0,1) = = "/")//is the root directory of the site
    12. echo "link". $i + 1). ":" $site. $REGARR [$i][1]. "
      ";//root directory
    13. Else
    14. echo "link". ($i + 1). ":" $base. $REGARR [$i][1]. "
      ";//Current directory
    15. Else
    16. echo "link". ($i + 1). ":" $regArr [$i][1]. "
      ";//relative path
    17. }
    18. fclose ($fp);
    19. ?

Form:http://www.uphtm.com/php/253.html

The above describes the PHP get all the links in the specified URL page, including the contents of the content, I hope the PHP tutorial interested in a friend helpful.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.