search
Homephp教程php手册php正则匹配获取指定url网页页面超级链接地址

php正则匹配获取指定url网页页面超级链接地址

Jun 13, 2016 am 11:32 AM
phpurlandanalyzematchexistaddressdesignationdata collectionregularWeb pageObtainneedpage

 在数据采集与页面分析中,常需要抓取给定url页面的内容,或者第二、第三层次深度页面内容。

这里是一个测试例子的实现,仅供参考。

代码如下:


/*
匹配给定页面链接
return:array match[link,content,all]
*/
function match_links($host, $document) {
 $pattern = '/(.*?)/i'; 
 preg_match_all($pattern, $document, $m); 
 return $m;

 preg_match_all("']+))[^>]*>?(.*?)'isx",$document,$links);
 while(list($key,$val) = each($links[2])) {
  if(!empty($val))
   if(preg_match("/http/",$val)){
    $match['link'][] = $val;
   }
   else {
    $match['link'][] = $host . $val;
   }
 }
 while(list($key,$val) = each($links[3])) {
  if(!empty($val))
   if(preg_match("/http/",$val)){
    $match['link'][] = $val;
   }
   else {
    $match['link'][] = $host . $val;
   }
 }
 while(list($key,$val) = each($links[4])) {
  if(!empty($val))
   $match['content'][] = $val;
 }
 while(list($key,$val) = each($links[0])) {
  if(!empty($val))
   $match['all'][] = $val;
 }
 return $match['link'];
}

/*
从给定url中获取页面文本内容
*/
function get_content_from_url($url) {
 $str = @file_get_contents($url);
 if(mb_check_encoding($str, "GBK"))
  $str = iconv("GBK","UTF-8", $str);
 $str = strip_tags($str); // 过滤html标签
/* 
 $str = preg_replace( "@<script>@is", "", $str );<br /> $str = preg_replace( "@<iframe(.*?)@is", "", $str );<br /> $str = preg_replace( "@<style(.*?)@is", "", $str );<br /> $str = preg_replace( "@<(.*?)>@is", "", $str );<br /> */<br /> //过滤非汉字字符<br /> preg_match_all('/[x{4e00}-x{9fff}]+/u', $str, $matches);<br /> $str = join(',', $matches[0]);<br /> if(!$str)<br /> return NULL;<br /> <br /> return $str;<br /> }</script>

function get_content($url,$depth) {
 if(!$url || $depth   return false;

 while($depth > 1){
  $str = @file_get_contents($url);
  if(!$str)
   return false;

  $parseurl = parse_url($url); 
  if($parseurl['host'])
   $host = $parseurl[scheme] . "://" . $parseurl['host'];
  
  $arrlink = match_links($host,$str);
  $arr_url = array_unique($arrlink);

  $depth--;
  foreach($arr_url as $url){
   $content .= get_content($url, $depth); //递归调用
  }
 }

 $content .= get_content_from_url($url);
  
 return $content;
}

Statement
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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft