php使用QueryList轻松采集JavaScript动态渲染页面-php教程

资源魔 32 0
QueryList应用jQuery的形式来做采集,领有丰厚的插件。

上面来演示QueryList应用PhantomJS插件抓取JS静态创立的页面内容。

保举:《PHP教程》

装置

应用Composer装置:

装置QueryList

composer require jaeger/querylist
GitHub: https://github.com/jae-jae/QueryList

装置PhantomJS插件

composer require jaeger/querylist-phantomjs
GitHub: https://github.com/jae-jae/QueryList-PhantomJS

下载PhantomJS二进制文件

PhantomJS官网:http://phantomjs.org ,下载对应平台的PhantomJS二进制文件。

插件API

QueryList browser($url,$debug = false,$co妹妹andOpt = []):应用阅读器关上衔接

应用

以采集「昔日头条」手机版为例,「昔日头条」手机版基于React框架,内容是纯静态衬着进去的。

上面演示QueryList的PhantomJs插件用法:

装置插件

use QL\QueryList;
use QL\Ext\PhantomJs;
$ql = QueryList::getInstance();
// 装置时需求设置PhantomJS二进制文件门路
$ql->use(PhantomJs::class,'/usr/local/bin/phantomjs');
//or Custom function name
$ql->use(PhantomJs::class,'/usr/local/bin/phantomjs','browser');

Example-1

猎取静态衬着的HTML:

$html = $ql->browser('https://m.toutiao.com')->getHtml();
print_r($html);

猎取一切p标签文本内容:

$data = $ql->browser('https://m.toutiao.com')->find('p')->texts();
print_r($data->all());

输入:

Array
(
    [0] => 自己拍照模式开启!国庆假期我以及国旗合个影
    [1] => 你旅途已开端 他们仍正在本人的岗亭上为你的假期保驾护航
    [2] => 喜极而泣,都传授终于回到地球了!
    //....
)

应用http代办署理:

// 更多选项能够查看文档: http://phantomjs.org/api/co妹妹and-line.html
$ql->browser('https://m.toutiao.com',true,[
    // 应用http代办署理
    '--proxy' => '192.168.1.42:8080',
    '--proxy-type' => 'http'
])

Example-2

自界说一个复杂的申请:

$data = $ql->browser(function (\JonnyW\PhantomJs\Http\RequestInterface $r){
    $r->setMethod('GET');
    $r->setUrl('https://m.toutiao.com');
    $r->setTimeout(10000); // 10 seconds
    $r->setDelay(3); // 3 seconds
    return $r;
})->find('p')->texts();
print_r($data->all());

开启debug模式,并从内陆加载cookie文件:

$data = $ql->browser(function (\JonnyW\PhantomJs\Http\RequestInterface $r){
    $r->setMethod('GET');
    $r->setUrl('https://m.toutiao.com');
    $r->setTimeout(10000); // 10 seconds
    $r->setDelay(3); // 3 seconds
    return $r;
},true,[
    '--cookies-file' => '/path/to/cookies.txt'
])->rules([
    'title' => ['p','text'],
    'link' => ['a','href']
])->query()->getData();
print_r($data->all());

以上就是php应用QueryList轻松采集JavaScript静态衬着页面的具体内容,更多请存眷资源魔其它相干文章!

标签: php php开发教程 php开发资料 php开发自学 QueryList

抱歉,评论功能暂时关闭!