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. ' <br/> ';
    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. ' <br/> ';
    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/";//need to collect the URL address of 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 URL Address page
  6. while (!feof ($FP)) $contents. =fread ($FP, 1024);
  7. $pattern = "|href=["]? ([^ ' \ "]+) [' \ ']| U ";
  8. Preg_match_all ($pattern, $contents, $REGARR, preg_set_order);//use regular match all href=
  9. for ($i =0; $i <count ($REGARR); $i + +) {//Find out all matching links
  10. if (!eregi ("://", $REGARR [$i][1])//Determine 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]. " <br/> ";//root directory
  13. Else
  14. echo "link". ($i + 1). ":" $base. $REGARR [$i][1]. " <br/> ";//Current directory
  15. Else
  16. echo "link". ($i + 1). ":" $regArr [$i][1]. " <br/> ";//relative path
  17. }
  18. Fclose ($FP);
  19. ?>

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

PHP gets all the links in the specified URL page

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.