PHP获取指定URL页面中的全部链接
PHP获取指定URL页面中的所有链接
form:http://www.uphtm.com/php/253.html
这个东西其实我们开发人员来讲常用了,以前做一个抓取其它网站友情连接时用过,今天看到一朋友整理了一个PHP获取指定URL页面中的所有链接函数,整理过来我们一起来看看吧。
以下代码可以获取到指定URL页面中的所有链接,即所有a标签的href属性:
- // 获取链接的HTML代码
- $html = file_get_contents('http://www.111cn.net');
- $dom = new DOMDocument();
- @$dom->loadHTML($html);
- $xpath = new DOMXPath($dom);
- $hrefs = $xpath->evaluate('/html/body//a');
- for ($i = 0; $i length; $i++) {
- $href = $hrefs->item($i);
- $url = $href->getAttribute('href');
- echo $url.'
'; - }
这段代码会获取到所有a标签的href属性,但是href属性值不一定是链接,我们可以在做个过滤,只保留http开头的链接地址:
- // 获取链接的HTML代码
- $html = file_get_contents('http://www.111cn.net');
- $dom = new DOMDocument();
- @$dom->loadHTML($html);
- $xpath = new DOMXPath($dom);
- $hrefs = $xpath->evaluate('/html/body//a');
- for ($i = 0; $i length; $i++) {
- $href = $hrefs->item($i);
- $url = $href->getAttribute('href');
- // 保留以http开头的链接
- if(substr($url, 0, 4) == 'http')
- echo $url.'
'; - }
fopen()函数读取指定网页中的所有链接并统计出数量,在一些需要采集网页页容的地方,适合使用本代码,本例以读取百度首页为例,找出百度首页中所有的链接地址,代码经过测试,完全可用:
- if(empty($url))$url = "http://www.baidu.com/";//需要采集链接的URL地址
- $site=substr($url,0,strpos($url,"/",8));
- $base=substr($url,0,strrpos($url,"/")+1);//文件所在目录
- $fp = fopen($url, "r" );//打开url地址页面
- while(!feof($fp))$contents.=fread($fp,1024);
- $pattern="|href=['\"]?([^ '\"]+)['\" ]|U";
- preg_match_all($pattern,$contents, $regArr, PREG_SET_ORDER);//使用正则匹配所有href=
- for($i=0;$i
- if(!eregi("://",$regArr[$i][1]))//判断是否是相对路径,即是否还有://
- if(substr($regArr[$i][1],0,1)=="/")//是否是站点的根目录
- echo "link".($i+1).":".$site.$regArr[$i][1]."
";//根目录- else
- echo "link".($i+1).":".$base.$regArr[$i][1]."
";//当前目录- else
- echo "link".($i+1).":".$regArr[$i][1]."
";//相对路径- }
- fclose($fp);
- ?>
form:http://www.uphtm.com/php/253.html

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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

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

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.

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

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

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

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

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