QueryList使用jQuery的方式来做采集,拥有丰富的插件。下面来演示QueryList使用PhantomJS插件抓取JS动态创建的页面内容。
一、安装
使用Composer安装:
1.安装QueryList
composer require jaeger/querylist
GitHub: https://github.com/jae-jae/QueryList
2.安装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,$commandOpt = []):使用浏览器打开连接
四、使用
以采集「今日头条」手机版为例,「今日头条」手机版基于React框架,内容是纯动态渲染出来的。
1.安装插件
$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');
// 安装时需要设置PhantomJS二进制文件路径
$ql->use(PhantomJs::class,'/usr/local/bin/phantomjs');
//or Custom function name
$ql->use(PhantomJs::class,'/usr/local/bin/phantomjs','browser');
2.Example-1
获取动态渲染的HTML:
browser('https://m.toutiao.com')->getHtml();
print_r($html);
browser('https://m.toutiao.com')->find('p')->texts();
print_r($data->all());
输出:
自拍模式开启!国庆假期我和国旗合个影
[1] => 你旅途已开始 他们仍在自己的岗位上为你的假期保驾护航
[2] => 喜极而泣,都教授终于回到地球了! //....)
使用http代理:
browser('https://m.toutiao.com',true,[
// 使用http代理
'--proxy' => '192.168.1.42:8080','--proxy-type' => 'http'
])
3.Example-2
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文件:
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;
},[
'--cookies-file' => '/path/to/cookies.txt'
])->rules([
'title' => ['p','text'],'link' => ['a','href']
])->query()->getData();
print_r($data->all());
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。