用户
 找回密码
 入住 CI 中国社区
搜索
查看: 13418|回复: 6
收起左侧

phpquery进行采集(原创)

[复制链接]
发表于 2012-10-5 11:52:39 | 显示全部楼层 |阅读模式
本帖最后由 ★わ浪漫少帅 于 2012-10-5 12:01 编辑
PHP复制代码
 
     //因为执行时间较长,所以设置运行时间不被限制---编码utf-8        ini_set('max_execution_time', '0');
        //ini_set('date.timezone','Asia/Taipei');
        require_once dirname(__FILE__).'./phpQuery.php';
        //比价网---採集
        $http = 'http://www.beargoo.com.cn/';
        $html = file_get_contents($http);
        phpquery::newDocumentHTML($html,'utf-8');
        //产品目录-----大类
        $categorys = pq('div.menu li>a.drop');
        foreach($categorys as $category){
                $category = pq($category);
                //大类名称---需数据库存储
                $category_name = $category->text();
                //大类---->子类产品---需数据库存储
                $anchors = $category->next()->find('ul>li>a');
                foreach($anchors as $anchor){
                        $anchor = pq($anchor);
                        //需数据库存储
                        $url = $anchor->attr('href');
//                        $type_url = parse_url($url);
//                        //产品类型
//                        $type = $type_url['query'];
                        //需数据库存储
                        $title = $anchor->text();
                        //在此处判断数据库中是否存在重复的记录,如果不存在,执行以下操作,存在就continue
                        $html = file_get_contents($url);
                        phpquery::newDocumentHTML($html,'utf-8');
                        //拿到全部的产品,模拟点击了全部按钮
                        $products_type = pq('dl.arrange')->find('dd:last');
                        preg_match_all("/\'(.*)\',\'(.*)\'/si",$products_type->attr('onclick'),$infos);
                        $param = $infos[1][0];
                        $var = $infos[2][0];
                        $all_url = $url.'&'.$param.'='.$var;
                        //重新发送请求
                        $html = file_get_contents($all_url);
                        phpquery::newDocumentHTML($html,'utf-8');
                        //获得产品的页数
                        $page_total = pq('div.pagination li:not(:last):last')->text();
                        //循环页数,拿到所有产品
                        for($i=1;$i<=$page_total;$i++){
                                $page_url = $all_url.'&page='.$i;
                                $html = file_get_contents($page_url);
                                phpquery::newDocumentHTML($html,'utf-8');
                                $product_lists = pq('div.list>div.productlist');
                                foreach($product_lists as $product_list){
                                        $product_list = pq($product_list);
                                        //产品url---需数据库存储
                                        $product_url = $product_list->find('dt>a')->attr('href');
                                        //产品名称---需数据库存储
                                        $product_title = $product_list->find('dt>a')->text();
                                        $html = file_get_contents($product_url);
                                        phpquery::newDocumentHTML($html,'utf-8');
                                        //比价网为您找到网销最低价---begin---需数据库存储
                                        $lower_url = pq('dl.proparam')->find('dd>a:first')->attr('href');
                                        $lower_price = pq('dl.proparam')->find('dd>a:first')->next()->text();
                                        $lower_title = pq('dl.proparam')->find('dd>a:first')->text();
                                        $detail_param_url = $http.pq('dl.proparam')->find('dd>a:last')->attr('href');
                                        //end
                                        //详细参数
                                        $html = file_get_contents($detail_param_url);
                                        phpquery::newDocumentHTML($html,'utf-8');
                                        $params_cate = pq('div.paramb>div.paramhead>dt');
                                        $params_paramk = pq('div.paramk');
                                        foreach($params_paramk as $k=>$param_paramk){
                                                $param_paramk = pq($param_paramk);
                                                //参数类别---需数据库存储
                                                $param_cate = $params_cate->eq($k);
                                                $paramks = $param_paramk->find('dl');
                                                foreach($paramks as $paramk){
                                                        $paramk = pq($paramk);
                                                        //参数名称---需数据库存储
                                                        $param_name = $paramk->find('dt')->text();
                                                        //参数值---需数据库存储
                                                        $param_value = $paramk->find('dd')->text();
                                                        //打印..此处做数据库保存工作..
                                                        exit($param_cate.$param_name.$param_value);
                                                }
                                        }
                                }
                        }
                }
        }
 
复制代码

评分

参与人数 1威望 +5 收起 理由
julia + 5 赞一个!

查看全部评分

发表于 2013-11-10 07:12:14 | 显示全部楼层
请问在 CI 中如何
? 存放PHPQuery
? 调用
 楼主| 发表于 2013-11-13 16:39:46 | 显示全部楼层
julia 发表于 2013-11-10 07:12
请问在 CI 中如何
? 存放PHPQuery
? 调用

可以放在libraries文件夹中作为一个插件调用
发表于 2013-11-18 12:48:34 | 显示全部楼层
多谢!
发表于 2014-2-12 12:17:04 | 显示全部楼层
比起snoopy哪个更高效呢?
 楼主| 发表于 2014-2-14 11:25:53 | 显示全部楼层
smartweb 发表于 2014-2-12 12:17
比起snoopy哪个更高效呢?

pq被国外认为是最快的php dom采集器,之后要衍生出htmlsql这种sql采集器,但速度远不如pq.而snoopy是一个http的解析器,模拟web浏览器,其实如果有安装curl,便可以做到snoopy的功能,
发表于 2015-1-27 16:39:58 | 显示全部楼层
请问楼主有phpquery的中文手册吗?由于本人还是新手,刚工作不久,源码又看不懂,然后网上关于phpquery的资料又比较少。

本版积分规则