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

yipeiwu_com6年前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程序设计有所帮助。

相关文章

curl实现站外采集的方法和技巧

选择curl的理由 关于curl与file_get_contents,摘抄一段通俗易懂的对比:file_get_contents其实是一堆内置的文件操作函数的合并版本,比如file_ex...

关于Appserv无法打开localhost问题的解决方法

安装了Appserv时,无法打开http://localhost或是http://127.0.0.1 在端口没有被占的情况下(本来我就没安装IIS),只要启动下D:\AppServ\Ap...

phpmyadmin中禁止外网使用的方法

本文实例讲述了phpmyadmin中禁止外网使用的方法。分享给大家供大家参考。具体方法如下: 首先,在phpmyadmin文件夹中找到 phpmyadmin.conf 在文件中能看到如下...

PHP获取指定月份第一天和最后一天的方法

本文实例讲述了PHP获取指定月份第一天和最后一天的方法。分享给大家供大家参考。具体如下: 复制代码 代码如下:$date = date(time()); $start_date = da...

Laravel中log无法写入问题的解决

前言 账号登录报500错误,也没有返回错误信息,没办法只能使用原始方法,到现在一行一行打印。到 Log::info() 后面就无法正常显示了,那么问题就找到了。 导致无法写入日志的问题,...