php实现递归抓取网页类实例

yipeiwu_com5年前PHP代码库

本文实例讲述了php实现递归抓取网页类的方法。分享给大家供大家参考。具体如下:

<?php
class crawler{
 private $_depth=5;
 private $_urls=array();
 function extract_links($url)
 {
  if(!$this->_started){
   $this->_started=1;
   $curr_depth=0;
  }else{
   $curr_depth++;
  }
  if($curr_depth<$this->_depth)
  {
   $data=file_get_contents($url);
   if(preg_match_all('/((?:http|https)://(?:www.)*(?:[a-zA-Z0-9_-]{1,15}.+[a-zA-Z0-9_]{1,}){1,}(?:[a-zA-Z0-9_/.-?&:%,!;]*))/',$data,$urls12))
   {
    foreach($urls12[0] as $k=>$v){
     $check=get_headers($v,1);
     if(strstr($v,$url) && $check[0]=='HTTP/1.1 200 OK' && !array_search($v,$this->_urls) && $curr_depth<$this->_depth){
      $this->_urls[]=$v;
      $this->extract_links($v);
     }
    }
   }
  }
  return $this->_urls;
 }
}
?>

希望本文所述对大家的php程序设计有所帮助。

相关文章

关于Zend Studio 配色方案插件的介绍

关于Zend Studio 配色方案插件的介绍

首先,菜单栏–help–install new software… 接着,添加一个更新源,就是点击界面的add按钮,在新窗口的location位置输入http://eclipse-col...

php实现转换html格式为文本格式的方法

本文实例讲述了php实现转换html格式为文本格式的方法。分享给大家供大家参考,具体如下: 有时候需要转换html格式的字符串为文本,但又需要保持一定的格式,比如要求段落变成的分段格式就...

Yii配置与使用memcached缓存的方法

本文实例讲述了Yii配置与使用memcached缓存的方法。分享给大家供大家参考,具体如下: 1. 下载memcached软件包,解压,把memcached.exe 放到随意一个地方,比...

php5数字型字符串加解密代码

<?php /* ----------------------------------------------------------------------------...

关于ob_get_contents(),ob_end_clean(),ob_start(),的具体用法详解

ob_get_contents();ob_end_clean();ob_start()使用ob_start()把输出那同输出到缓冲区,而不是到浏览器。然后用ob_get_contents...