您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息
三六零分类信息网 > 巴中分类信息网,免费分类信息发布

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

2024/3/9 23:46:55发布18次查看
一、采集类源码
复制代码 代码如下:
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);
         }
}
二、使用例子
复制代码 代码如下:
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 .
;//采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);
//就举这么多例子吧,是不是用来做采集很方便
http://www.bkjia.com/phpjc/751515.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/751515.htmltecharticle一、采集类源码 复制代码 代码如下: ?php /** *通用列表采集类 *版本v1.3 *作者:jae */ require_once '../phpquery/phpquery/phpquery.php'; class querylist{ priva...
巴中分类信息网,免费分类信息发布

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录