欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

一个基于phpQuery的php通用采集类分享

程序员文章站 2022-07-21 14:04:34
一、采集类源码复制代码 代码如下:

一、采集类源码

复制代码 代码如下:

<?php
  /**
  *通用列表采集类
  *版本v1.3
  *作者:jae
  */
    require_once '../phpquery/phpquery/phpquery.php';
    class querylist{

        private $pageurl;
         private $regarr = array();
         public $jsonarr = array();
         private $regrange;
         private $html;
         /************************************************
         * 参数: 页面地址 选择器数组 块选择器
         * 【选择器数组】说明:格式array("名称"=>array("选择器","类型"),.......)
         * 【类型】说明:值 "text" ,"html" ,"属性"
         *【块选择器】:指 先按照规则 选出 几个大块 ,然后再分别再在块里面 进行相关的选择
         *************************************************/
         function querylist($pageurl,$regarr=array(),$regrange='')
         {
             $this->pageurl = $pageurl;

             //为了能获取https://
               $ch = curl_init();
                curl_setopt($ch, curlopt_url,$this->pageurl);
                curl_setopt($ch, curlopt_ssl_verifypeer, false);
                curl_setopt($ch, curlopt_ssl_verifyhost, false);
                curl_setopt($ch,curlopt_returntransfer,1);
                $this->html = curl_exec($ch);
               curl_close($ch);

             if(!empty($regarr))
             {

                  $this->regarr = $regarr;
                 $this->regrange = $regrange;
                 $this->getlist();
             }

         }
         function setquery($regarr,$regrange='')
         {
             $this->jsonarr=array();
             $this->regarr = $regarr;
             $this->regrange = $regrange;
             $this->getlist();
         }
        private function getlist()
         {

             $hobj = phpquery::newdocumenthtml($this->html);
             if(!empty($this->regrange))
             {
             $robj = pq($hobj)->find($this->regrange);

              $i=0;
             foreach($robj as $item)
             {

                 while(list($key,$reg_value)=each($this->regarr))
                 {
                     $iobj = pq($item)->find($reg_value[0]);

                       switch($reg_value[1])
                       {
                           case 'text':
                                 $this->jsonarr[$i][$key] = trim(pq($iobj)->text());
                                 break;
                           case 'html':
                                 $this->jsonarr[$i][$key] = trim(pq($iobj)->html());
                                 break;
                           default:
                                $this->jsonarr[$i][$key] = pq($iobj)->attr($reg_value[1]);
                                break;

                        }
                 }
                 //重置数组指针
                 reset($this->regarr);
                 $i++;
              }
             }
             else
             {
            while(list($key,$reg_value)=each($this->regarr))
             {
                $lobj = pq($hobj)->find($reg_value[0]);

                  
                   $i=0;
                   foreach($lobj as $item)
                   {
                       switch($reg_value[1])
                       {
                           case 'text':
                                 $this->jsonarr[$i++][$key] = trim(pq($item)->text());
                                 break;
                           case 'html':
                                 $this->jsonarr[$i++][$key] = trim(pq($item)->html());
                                 break;
                           default:
                                $this->jsonarr[$i++][$key] = pq($item)->attr($reg_value[1]);
                                break;

                        }

                    
                   }

       
             }
           }
         } 
         function getjson()
         {
             return json_encode($this->jsonarr);
         }

}

二、使用例子

复制代码 代码如下:

<?php
require 'query/querylist.class.php';


//采集osc的代码分享列表,标题 链接 作者
$url = "http://www.oschina.net/code/list";
$reg = array("title"=>array(".code_title a:eq(0)","text"),"url"=>array(".code_title a:eq(0)","href"),"author"=>array("img","title"));
$rang = ".code_list li";
$hj = new querylist($url,$reg,$rang);
$arr = $hj->jsonarr;
print_r($arr);
//如果还想采当前页面右边的 top40活跃贡献者 图像,得到json数据,可以这样写
$reg = array("portrait"=>array(".hot_top img","src"));
$hj->setquery($reg);
$json = $hj->getjson();
echo $json . "<hr/>";

//采osc内容页内容
$url = "http://www.oschina.net/code/snippet_186288_23816";
$reg = array("title"=>array(".qtitle h1","text"),"con"=>array(".content","html"));
$hj = new querylist($url,$reg);
$arr = $hj->jsonarr;
print_r($arr);

//就举这么多例子吧,是不是用来做采集很方便