php网页采集 测试版

<?php
header("content-type: text/html; charset=utf-8");
class HttpWrap
{


    public $timeout=3;
    public $status='';



    public $host;
    public $port=80;
    private $conn;
    private $path;
    private $url;

    public $http_method='GET';
    public $http_version="HTTP/1.1";
    public $agent="Mozilla/5.0 (Windows NT 6.1; rv:33.0) Gecko/20100101 Firefox/33.0";
    public $accept="image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*";
    public $gzip="gzip";
    public $referer;
    public $cookie;
    public $submit_type="application/x-www-form-urlencoded";
    private $accept_language="zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3";
    public $connection="keep-alive";


    private $cmd_line;
    private $header;
    public $post_content;

    private $redirect;
    private $is_gzip;

    public $response_num;
    public $response_header;
    public $response_body_length=0;
    public $response_body;

    public $roll_link;

   public  function init($url)
    {
        $this->url=$url;
        $url_pair = parse_url($url);
        $this->host = $url_pair['host'];
        $this->path = $url_pair['path'];

        if(!empty($url_pair['port']))
        {
            $this->port = $url_pair['port'];
        }
        $this->connect();
       // echo $this->status; exit();
        $this->sendRequest();
        //如果响应头部存在重定向,则对重定向发送请求
        if($this->redirect)
        {
            if(preg_match("#^http://".preg_quote($this->host)."#i",$this->redirect))
            {
                $this->referer=$this->host."/".parse_url($this->redirect)['path'];
                $this->init($this->redirect);
            }
        }
        if($this->roll_link)
        {
            $next_url = substr($this->url,0,strripos($this->url, '/')+1).$this->roll_link;
            if(strtolower(trim($this->url)) == strtolower(trim($next_url))) die("本页截取完毕<br />");
            echo str_repeat("  ", 2048);
            echo "对链接".$next_url."发起请求<br />";
            sleep(1);
            $this->init($next_url);
        }

    }

   private function connect()
   {
       $this->conn = fsockopen($this->host,$this->port,$errno,$errstr,$this->timeout);
       if($this->conn)
       {
           $this->status = '链接成功';
           return true;
       }
       else
       {
            switch($errno)
            {
                case -3:
                        $this->status="创建socket链接失败";
                case -4:
                        $this->status="dns查询失败";
                case -5:
                        $this->status="链接被拒绝或超时";
                default:
                        $this->status="创建连接失败";
            }
            return false;
       }
   }
   private function sendRequest()
   {
       if(empty($this->path))
       {
           $this->path="/";
       }
       $this->cmd_line=$this->http_method." ".$this->path." ".$this->http_version."\r\n";

       if(!empty($this->host))
       {
           $this->header .= "Host: ".$this->host."\r\n";
       }

       if(!empty($this->agent))
       {
           $this->header .="User-Agent: ".$this->agent."\r\n";
       }

       if(!empty($this->accept))
       {
           $this->header .= "Accept: ". $this->accept ."\r\n";
       }
       if(!empty($this->gzip))
       {
           if ( function_exists("gzinflate") )
           {
                $this->header .= "Accept-encoding: gzip\r\n";
            }
            else
            {
                $this->status = "不支持压缩";
            }
       }
       if(!empty($this->referer))
       {
           $this->header .= "Referer: ".$this->referer."\r\n";
       }
       if(!empty($this->accept_language))
       {
           $this->header .= "Accept-Language: ".$this->accept_language."\r\n";
       }
       if(!empty($this->cookie))
       {
           if(!is_array($this->cookie))
           {
               $this->header .="Cookie: ".$this->cookie;
           }
           else
           {
               if(count($this->cookie) >0)
               {
                   $cookie = "Cookie: ";
                   foreach($this->cookie as $key => $val)
                   {
                       $cookie.=$key."=".urlencode($val).";";
                   }
                  $cookie = substr($cookie, 0, strlen($cookie)-1)."\r\n";
               }
               $this->header .= $cookie;
           }
       }
       if(!empty($this->submit_type))
       {
           $this->header .="Content-Type: ".$this->submit_type."\r\n";
       }
       if(!empty($this->post_content))
       {
           $this->header .= "Content-length: ".strlen($this->post_content)."\r\n";
       }
       if(!empty($this->connection))
       {
           $this->header .= "Connection: ".$this->connection."\r\n";
       }
       $this->header .="\r\n";
       //上面是HTTP请求头部信息
       //echo $this->cmd_line.$this->header.$this->post_content; exit();
       //发送请求
       $len = strlen($this->cmd_line.$this->header.$this->post_content);
      if($len != fwrite($this->conn, $this->cmd_line.$this->header.$this->post_content,$len))
      {
          $this->status = "发送请求failed";
      }

       //接受响应,每次读取一行内容,首先解析响应头
       while($response_header = fgets($this->conn, 1024))
       {
           if(preg_match("|^HTTP/|",$response_header))
            {
                //匹配状态数字,200表示请求成功
                if(preg_match("|^HTTP/[^\s]*\s(.*?)\s|",$response_header, $status))
                {
                        $this->response_num= $status[1];//返回代表数字的状态
                }
            }
            //echo $this->response_num; exit();
            // 判断是否需要重定向
            if(preg_match("#^(Location:|URI:)#i",$response_header))
            {
                // 获取重定向地址
                preg_match("#^(Location:|URI:)\s+(.*)#",trim($response_header),$matches);

                //如果重定向字段不包含主机名,不是以以://开头的,则拼接王完整的请求地址,模式+主机+端口
                if(!preg_match("#\:\/\/#",$matches[2]))
                {
                    // 补全主机名
                    $this->redirect = "http://".$this->host.":".$this->port;

                    //添加路径
                    if(!preg_match("|^/|",$matches[2]))
                           $this->redirect .= "/".$matches[2];
                    else
                           $this->redirect .= $matches[2];
                }
                else
                //包含完整的主机地址
                        $this->redirect = $matches[2];
            }

        //判断返回的数据的压缩格式
	if (preg_match("#^Content-Encoding: gzip#", $response_header) )
          {
                $this->is_gzip = true;
          }
          if(preg_match('#^Content-Length:\s*(\d+)#i', $response_header, $len))
          {
              $this->response_body_length = $len[1];
          }

        //解析完响应头部
        if(preg_match("/^\r?\n$/", $response_header) )
            break;

        $this->response_header[]=$response_header;
       }
       //可以成功返回响应头部信息,响应状态码也为200
      // var_dump($this->response_header); exit();

        if($this->response_num==200)
        {
            //问题出在这里
            //echo "ok"; exit();
            $dirname = "./download/".date("Ymd");
            $path;
            $filename;
            $len=0;
            while($items = fread($this->conn, $this->response_body_length))
            {
                if(!is_dir($dirname))
                {
                    $path = mkdir($dirname,0777,true);
                }
                $filename = $dirname.'/'.basename($this->url,'.html').'.txt';
                $len = $len+strlen($items);
                file_put_contents($filename, $items, FILE_APPEND);
                //这里必须判断读取的长度,不然会在这里阻塞
                if($len >= $this->response_body_length) break;

            }

            if($this->is_gzip)
                $this->response_body = gzinflate ($this->response_body);
            $this->getRollLink($filename);
           // sleep(1);
        }
   }
    private function getRollLink($filename)
   {
       if(!file_exists($filename)) die('文件不存在');
       $temp=file_get_contents($filename, false, null, 0, 64);
        $encoding=mb_detect_encoding($temp, array('GB2312','GBK','UTF-8','BIG5','LATIN1'));
        $content='';
        if($encoding !='UTF-8')
        {
                $content = mb_convert_encoding(file_get_contents($filename), 'UTF-8', $encoding);
        }

       if(preg_match('#<ul\s+class="image"[^>]*?>.*?</ul>#is', $content, $match))
       {
           if(preg_match('#<a\s+href="([^"]+?)">下一页</a>#ui', $match[0], $next))
           {
                $this->roll_link =  $next[1];
           }
       }
       else
       {
          $this->roll_link = false;
       }
   }

}
ob_implicit_flush(true);
set_time_limit(0);
$url = $url = "http://www.mmkao.com/Beautyleg/201410/6503_2.html";
$http = new HttpWrap();
$http->cookie = "safedog-flow-item=41E2DBFEF121A8A2835ADB4476E5D3EC";
$http->referer = "www.mmkao.com";
$http->init($url);
?>
</pre><pre code_snippet_id="544502" snippet_file_name="blog_20141206_3_7561862" name="code" class="php">//测试完成对10个分页采集完成,稍加修改就能完成整站采集,采集的都是HTML源码,速度比较快,为了避免被网站管理员发现,可以适当的设置休眠时间,模拟人为浏览,
//有些网站的当前页的分页数字的链接是指向当前页的,所以在最后一页,会反复发送同一个请求,所以必须判断,就是这里:
if(strtolower(trim($this->url)) == strtolower(trim($next_url))) die("本页截取完毕<br />");
//再就是有些网站有代理服务器,或者会有一些重定向的跳转,所以必须加以判断,
//如果采集向猫扑网论坛里面的论坛那样的网页,会存在frame,所以特殊情况,可以加以判断




采集过程记录:

对链接http://www.mmkao.com/Beautyleg/201410/6503_2.html发起请求
对链接http://www.mmkao.com/Beautyleg/201410/6503_3.html发起请求
对链接http://www.mmkao.com/Beautyleg/201410/6503_4.html发起请求
对链接http://www.mmkao.com/Beautyleg/201410/6503_5.html发起请求
对链接http://www.mmkao.com/Beautyleg/201410/6503_6.html发起请求
对链接http://www.mmkao.com/Beautyleg/201410/6503_7.html发起请求
对链接http://www.mmkao.com/Beautyleg/201410/6503_8.html发起请求
对链接http://www.mmkao.com/Beautyleg/201410/6503_9.html发起请求
对链接http://www.mmkao.com/Beautyleg/201410/6503_10.html发起请求
采集完成



当然在本地也生成了对应的html网页

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值