Home Backend Development PHP Tutorial PHP获取指定URL页面中的全部链接

PHP获取指定URL页面中的全部链接

Jun 13, 2016 pm 12:18 PM
href html nbsp url

PHP获取指定URL页面中的所有链接

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

这个东西其实我们开发人员来讲常用了,以前做一个抓取其它网站友情连接时用过,今天看到一朋友整理了一个PHP获取指定URL页面中的所有链接函数,整理过来我们一起来看看吧。

以下代码可以获取到指定URL页面中的所有链接,即所有a标签的href属性:

  1. // 获取链接的HTML代码
  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 length; $i++) {
  8.    $href = $hrefs->item($i);
  9.    $url = $href->getAttribute('href');
  10.    echo $url.'
    ';
  11. }

这段代码会获取到所有a标签的href属性,但是href属性值不一定是链接,我们可以在做个过滤,只保留http开头的链接地址:

 

  1. // 获取链接的HTML代码
  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 length; $i++) {
  8.    $href = $hrefs->item($i);
  9.    $url = $href->getAttribute('href');
  10.   
  11.    // 保留以http开头的链接
  12.    if(substr($url, 0, 4) == 'http')
  13.       echo $url.'
    ';
  14. }

fopen()函数读取指定网页中的所有链接并统计出数量,在一些需要采集网页页容的地方,适合使用本代码,本例以读取百度首页为例,找出百度首页中所有的链接地址,代码经过测试,完全可用:

  1. if(empty($url))$url = "http://www.baidu.com/";//需要采集链接的URL地址
  2. $site=substr($url,0,strpos($url,"/",8));
  3. $base=substr($url,0,strrpos($url,"/")+1);//文件所在目录
  4. $fp = fopen($url, "r" );//打开url地址页面
  5. while(!feof($fp))$contents.=fread($fp,1024);
  6. $pattern="|href=['\"]?([^ '\"]+)['\" ]|U";
  7. preg_match_all($pattern,$contents, $regArr, PREG_SET_ORDER);//使用正则匹配所有href=
  8. for($i=0;$i
  9. if(!eregi("://",$regArr[$i][1]))//判断是否是相对路径,即是否还有://
  10.     if(substr($regArr[$i][1],0,1)=="/")//是否是站点的根目录
  11.     echo "link".($i+1).":".$site.$regArr[$i][1]."
    ";//根目录
  12.     else
  13.     echo "link".($i+1).":".$base.$regArr[$i][1]."
    ";//当前目录
  14. else
  15.     echo "link".($i+1).":".$regArr[$i][1]."
    ";//相对路径
  16. }
  17. fclose($fp);
  18. ?>

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

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

See all articles