如何用QueryList在PHP中高效采集JavaScript动态加载网页内容?

更新于
2026-09-30 13:33:12
1阅读来源:SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计710个文字,预计阅读时间需要3分钟。

如何用QueryList在PHP中高效采集JavaScript动态加载网页内容?

QueryList 是一个使用 jQuery 方式进行采集的工具,拥有丰富的插件。下面展示如何使用 PhantomJS 插件抓取动态创建的页面内容。

推荐:使用《PHP 教程》进行学习。

安装与使用 Composer:

1.安装 Composer:[https://getcomposer.org/](https://getcomposer.org/)

2.使用 Composer 安装 QueryList:`composer require querylist/querylist`

php

use QL\Parser\PhantomJs;

// 创建 PhantomJS 实例$phantom=new PhantomJs();// 设置 PhantomJS 路径$phantom->setPath('/path/to/phantomjs');

// 设置 PhantomJS 选项$phantom->setOption([ 'webSecurity'=> false, 'loadImages'=> false,]);

// 执行抓取$phantom->open('http://example.com');$dom=$phantom->evaluate('document', function ($dom) { return $dom->body->();});

// 输出结果echo $dom;

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

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

推荐:《PHP教程》

安装

如何用QueryList在PHP中高效采集JavaScript动态加载网页内容?

使用Composer安装:

安装QueryList

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

安装PhantomJS插件

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

下载PhantomJS二进制文件

PhantomJS官网: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('m.toutiao.com')->getHtml(); print_r($html);

获取所有p标签文本内容:

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

输出:

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

使用phantomjs.org/api/command-line.html $ql->browser('m.toutiao.com',true,[ // 使用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('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动态渲染页面的详细内容,更多请关注自由互联其它相关文章!

本文共计710个文字,预计阅读时间需要3分钟。

如何用QueryList在PHP中高效采集JavaScript动态加载网页内容?

QueryList 是一个使用 jQuery 方式进行采集的工具,拥有丰富的插件。下面展示如何使用 PhantomJS 插件抓取动态创建的页面内容。

推荐:使用《PHP 教程》进行学习。

安装与使用 Composer:

1.安装 Composer:[https://getcomposer.org/](https://getcomposer.org/)

2.使用 Composer 安装 QueryList:`composer require querylist/querylist`

php

use QL\Parser\PhantomJs;

// 创建 PhantomJS 实例$phantom=new PhantomJs();// 设置 PhantomJS 路径$phantom->setPath('/path/to/phantomjs');

// 设置 PhantomJS 选项$phantom->setOption([ 'webSecurity'=> false, 'loadImages'=> false,]);

// 执行抓取$phantom->open('http://example.com');$dom=$phantom->evaluate('document', function ($dom) { return $dom->body->();});

// 输出结果echo $dom;

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

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

推荐:《PHP教程》

安装

如何用QueryList在PHP中高效采集JavaScript动态加载网页内容?

使用Composer安装:

安装QueryList

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

安装PhantomJS插件

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

下载PhantomJS二进制文件

PhantomJS官网: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('m.toutiao.com')->getHtml(); print_r($html);

获取所有p标签文本内容:

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

输出:

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

使用phantomjs.org/api/command-line.html $ql->browser('m.toutiao.com',true,[ // 使用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('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动态渲染页面的详细内容,更多请关注自由互联其它相关文章!