VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > PHP >
  • 一个基于phpQuery的php通用采集类分享

这是自己以前写的一个php的采集类,自己一直在用,自我感觉很简单很强大,只要懂一点点选择器的知识就可以采集任何页面了,也支持https页面,做简单的采集足够用了。

一、采集类源码,代码如下:

  1. <?php 
  2.   /** 
  3.   *通用列表采集类 
  4.   *版本V1.3 
  5.   *作者:JAE 
  6.   */ 
  7.     require_once '../phpQuery/phpQuery/phpQuery.php'
  8.     class QueryList{ 
  9.  
  10.         private $pageURL
  11.          private $regArr = array(); 
  12.          public $jsonArr = array(); 
  13.          private $regRange
  14.          private $html
  15.          /************************************************ 
  16.          * 参数: 页面地址 选择器数组 块选择器 
  17.          * 【选择器数组】说明:格式array("名称"=>array("选择器","类型"),.......) 
  18.          * 【类型】说明:值 "text" ,"html" ,"属性"  
  19.          *【块选择器】:指 先按照规则 选出 几个大块 ,然后再分别再在块里面 进行相关的选择 
  20.          *************************************************/ 
  21.          function QueryList($pageURL,$regArr=array(),$regRange=''
  22.          { 
  23.              $this->pageURL = $pageURL
  24.  
  25.              //为了能获取https:// 
  26.                $ch = curl_init(); 
  27.                 curl_setopt($ch, CURLOPT_URL,$this->pageURL); 
  28.                 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);  
  29.                 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); 
  30.                 curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);  
  31.                 $this->html = curl_exec($ch); 
  32.                curl_close($ch); 
  33.  
  34.              if(!emptyempty($regArr)) 
  35.              { 
  36.  
  37.                   $this->regArr = $regArr
  38.                  $this->regRange = $regRange
  39.                  $this->getList(); 
  40.              } 
  41.  
  42.          } 
  43.          function setQuery($regArr,$regRange=''
  44.          { 
  45.              $this->jsonArr=array(); 
  46.              $this->regArr = $regArr
  47.              $this->regRange = $regRange
  48.              $this->getList(); 
  49.          } 
  50.         private function getList() 
  51.          { 
  52.  
  53.              $hobj = phpQuery::newDocumentHTML($this->html); 
  54.              if(!emptyempty($this->regRange)) 
  55.              { 
  56.              $robj = pq($hobj)->find($this->regRange); 
  57.  
  58.               $i=0; 
  59.              foreach($robj as $item
  60.              { 
  61.  
  62.                  while(list($key,$reg_value)=each($this->regArr)) 
  63.                  { 
  64.                      $iobj = pq($item)->find($reg_value[0]); 
  65.  
  66.                        switch($reg_value[1]) 
  67.                        { 
  68.                            case 'text'
  69.                                  $this->jsonArr[$i][$key] = trim(pq($iobj)->text()); 
  70.                                  break
  71.                            case 'html'
  72.                                  $this->jsonArr[$i][$key] = trim(pq($iobj)->html()); 
  73.                                  break
  74.                            default
  75.                                 $this->jsonArr[$i][$key] = pq($iobj)->attr($reg_value[1]); 
  76.                                 break
  77.  
  78.                         } 
  79.                  } 
  80.                  //重置数组指针 
  81.                  reset($this->regArr); 
  82.                  $i++; 
  83.               } 
  84.              } 
  85.              else 
  86.              { 
  87.             while(list($key,$reg_value)=each($this->regArr)) 
  88.              { 
  89.                 $lobj = pq($hobj)->find($reg_value[0]); 
  90.  
  91.                     
  92.                    $i=0; 
  93.                    foreach($lobj as $item
  94.                    { 
  95.                        switch($reg_value[1]) 
  96.                        { 
  97.                            case 'text'
  98.                                  $this->jsonArr[$i++][$key] = trim(pq($item)->text()); 
  99.                                  break
  100.                            case 'html'
  101.                                  $this->jsonArr[$i++][$key] = trim(pq($item)->html()); 
  102.                                  break
  103.                            default
  104.                                 $this->jsonArr[$i++][$key] = pq($item)->attr($reg_value[1]); 
  105.                                 break
  106.  
  107.                         } 
  108.  
  109.                       
  110.                    } 
  111.  
  112.          
  113.              } 
  114.            } 
  115.          }   
  116.          function getJSON() 
  117.          { 
  118.              return json_encode($this->jsonArr); 
  119.          }  
  120.  

二、使用例子,代码如下:

  1. <?php 
  2. require 'Query/QueryList.class.php'
  3.  
  4.  
  5. //采集OSC的代码分享列表,标题 链接 作者 
  6. $url = "http://www.oschina.net/code/list"
  7. $reg = array("title"=>array(".code_title a:eq(0)","text"),"url"=>array(".code_title a:eq(0)","href"),"author"=>array("img","title")); 
  8. $rang = ".code_list li"
  9. $hj = new QueryList($url,$reg,$rang); 
  10. $arr = $hj->jsonArr; 
  11. print_r($arr); 
  12. //如果还想采当前页面右边的 TOP40活跃贡献者 图像,得到JSON数据,可以这样写 
  13. $reg = array("portrait"=>array(".hot_top img","src")); 
  14. $hj->setQuery($reg); 
  15. $json = $hj->getJSON(); 
  16. echo $json . "<hr/>"
  17.  
  18. //采OSC内容页内容 
  19. $url = "http://www.oschina.net/code/snippet_186288_23816"
  20. $reg = array("title"=>array(".QTitle h1","text"),"con"=>array(".Content","html")); 
  21. $hj = new QueryList($url,$reg); 
  22. $arr = $hj->jsonArr; 
  23. print_r($arr); 

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

出处:http://www.phpfensi.com/php/20201108/13422.html

 


相关教程