首页> 实战笔录 >PHP开发笔记 >ThinkPHP ThinkPHP

ThinkPHP集成QueryList和PhantomJS插件实现采集动态加载的页面的数据

作者:小萝卜 2019-08-17 浏览 2215

简介ThinkPHP集成QueryList和PhantomJS插件来实现对javascript动态加载的页面的数据进行采集

QueryList文档地址https://querylist.cc/docs/guide/v4/overview
PhantomJS文档地址https://querylist.cc/docs/guide/v4/PhantomJS

代码示例:

< ?php
namespace Home\index\controller;
use think\Controller;
use QL\QueryList;
use QL\Ext\PhantomJs;
class Gather extends Controller {
    
    public function gathering(){

    	$ql = QueryList::getInstance();
		// 安装时需要设置PhantomJS二进制文件路径
		//$ql->use(PhantomJs::class,ROOT_PATH.'/Pulg/PhantomJS/Linux-64/phantomjs');
		//or Custom function name 或一个自定义函数的名称
		//$ql->use(PhantomJs::class,ROOT_PATH.'/Pulg/PhantomJS/Linux-64/phantomjs','browser');

		// Windows下示例
		// 注意:路径里面不能有空格中文之类的
		$ql->use(PhantomJs::class,ROOT_PATH.'Pulg/PhantomJS/Windows/phantomjs.exe');	

       //采集某页面所有的图片
		echo '开始获取图片< br>';
       $data = $ql->browser('https://sothebysrealty.ca/zh/property/quebec/montreal-real-estate/ville-marie/434021/')
                  ->rules([
			        // 爬取图片地址
			        "src"=>array("div.owl-lazy","data-src"), 
			    ])
			    ->query()->getData();

       //打印结果
       $photo=$data->all();
       var_dump($photo);
       echo '获取到图片< br>';
       echo '开始获取主要信息< br>';
       //采集某页面所有的图片
       $data = $ql->browser('https://sothebysrealty.ca/zh/property/quebec/montreal-real-estate/ville-marie/434021/')
                  ->rules([
			        // 标题
			        "title"=>array("div.detail-head span.span8 h1","text"),
			        // 地址
			        "address"=>array("div.detail-head span.span8 p","text"),
			        // 金额
			        "price"=>array("div.detail-head span.span4 ul.price_social li:eq(0)","text"),
			        //卧室
			        "bedroom"=>array("div.detail-head span.span4 ul.quick_facts li:eq(0)","text"),
			        //浴室
			        "bathroom"=>array("div.detail-head span.span4 ul.quick_facts li:eq(1)","text"),
			        //面积
			        "size"=>array("div.detail-head span.span4 ul.quick_facts li:eq(2)","text"),
			        //内容
			        "content"=>array("div.listing_content div.mod_content p",'html'),
			        
			    ])
			    ->query()->getData();

	    //打印结果
        $zdata=$data->all();
        var_dump($zdata);
		echo '获取主要信息< br>';
        echo '开始获取基础信息< br>';
	    //主要信息
	    $data = $ql->browser('https://sothebysrealty.ca/zh/property/quebec/montreal-real-estate/ville-marie/434021/')
                  ->rules([
			        //主要信息
			        "desc"=>array('div.mod_content ul.key_facts li','text')
			        
			    ])
			    ->query()->getData();		    

       //打印结果
       $jdata=$data->all();
       var_dump($jdata);
       echo '获取基础信息< br>';
       
       echo '开始获取房源特征< br>';
       //主要信息
	    $data = $ql->browser('https://sothebysrealty.ca/zh/property/quebec/montreal-real-estate/ville-marie/434021/')
                  ->rules([
			        //主要信息
			        "desc"=>array('div.listing_facts div.mod_content ul.all_amenities li','text')
			        
			    ])
			    ->query()->getData();		    

       //打印结果
       $tdata=$data->all();
       var_dump($tdata);
       echo '获取到房源特征< br>';
        exit;
    }

很赞哦! (0)

文章评论

    高端网站建设