QueryList使用jQuery的方式来做采集,拥有丰富的插件。

下面来演示QueryList使用PhantomJS插件抓取JS动态创建的页面内容。

推荐:《PHP教程》

安装

使用Composer安装:

安装QueryList

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

安装PhantomJS插件

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

下载PhantomJS二进制文件

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

插件API

QueryList browser($url,$debug = false,$commandOpt = []):使用浏览器打开连接

使用

以采集「今日头条」手机版为例,「今日头条」手机版基于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/command-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());

更多相关文章

  1. 分享一款免费开源的在线文档管理插件
  2. 还在使用第三方Docker插件?SpringBoot官方插件真香!
  3. 还在手动部署SpringBoot应用?试试这个自动化插件!
  4. RabbitMQ实现延迟消息居然如此简单,整个插件就完事了!
  5. 我常用的IDEA插件大公开,个个是精品!
  6. Mybatis框架如何使用分页插件呢?
  7. 使用Maven插件构建Docker镜像
  8. API注入机制及插件启动流程_VSCode插件开发笔记2
  9. IDEA常用设置及推荐插件

随机推荐

  1. Android如何查看应用签名信息--微信平台
  2. List of Android MIME types and Uri's
  3. Android中使用Junit测试框架
  4. Android获取长按按键响应
  5. Android(安卓)display架构分析(七-1)
  6. Android(安卓)Handler removeMessages引
  7. Android视图篇之一:Android常见基本布局
  8. Android应用开发相关下载资源(2013/03/22
  9. Android新增音频流类型
  10. Android error: Failed to install *.apk