querylist使用jquery的方式来做采集,拥有丰富的插件。
下面来演示querylist使用phantomjs插件抓取js动态创建的页面内容。
安装
使用compor安装:
氢气化学式安装querylist
1
2
compor require jaeger/querylist
github: https://github.com/jae-jae/querylist
安装phantomjs插件
1
2
compor require jaeger/querylist-phantomjs
github: https://github.com/jae-jae/querylist-phantomjs
下载phant早发白帝城唐诗omjs二进制文件
phantomjs官网:http://phantomjs.org ,下载对应平台的phantomjs二进制文件。
插件api
querylist browr($url,$debug = fal,$commandopt = []):使用浏览器打开连接
使用
以采集「今日头条」手机版为例,「今日头条」手机版基于react框架,内容是纯动态渲染出来的。
下面演示querylist的phantomjs插件用法:
安装插件
1
2
3
4
5
6
7
u ql\querylist;
u ql\ext\phantomjs;
$ql = querylist::getinstance退房申请();
// 安装时需要设置phantomjs二进制文件路径
$ql->u(phantomjs::class,'/usr/local/bin/phantomjs');
//or custom function name
$ql->u(phantomjs::class请帖模板结婚请柬,'/usr/local/bin/phantomjs','browr');
example-1
获取动态渲染的html:
1
2
$html = $ql->browr('https://m.toutiao.com')->gethtml();
print_r($html);
获取所有p标签文本内容:
1
2
$data = $ql->browr('https://m.toutiao.com')->find('p')->texts();
print_r($data->all());
输出:
1
2
3
4
5
6
7
array
(
[0] => 自拍模式开启!国庆假期我和国旗合个影
[1] => 你旅途已开始 他们仍在自己的岗位上为你的假期保驾护航
[2] => 喜极而泣,都教授终于回到地球了!
//....
)
使用http代理:
1
2
3
4
5
6
// 更多选项可以查看文档: http://phantomjs.org/api/command-line.html
$ql->browr('https://m.toutiao.com',true,[
// 使用http代理
'--proxy' => '192.168.1.42:8080',
'--proxy-type' => 'http'
])
example-2
自定义一个复杂的请求:
1
2
3
4
5
6
7
8
$data = $ql->browr(function (\jonnyw\phantomjs\http\requestinterface $r){
$r->tmethod('get');
$r->turl('https://m.toutiao.com');
$r->ttimeout(10000); // 10 conds
$r->tdelay(3); // 3 conds
return $r;
})->find('p')->texts();
print_r($data->all());
开启debug模式,并从本地加载cookie文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
$data = $ql->browr(function (\jonnyw\phantomjs\http\requestinterface $r){
$r->tmethod('get');
$r->turl('https://m.toutiao.com');
$r->ttimeout(10000); /英雄联盟寡妇制造者出装/ 10 conds
$r->tdelay(3); // 3 conds
return $r;
},true,[
'--cookies-file' => '/path/to/cookies.txt'
])->rules([
'title' => ['p','text'],
'link' => ['a','href']
])->query()->getdata();
print_r($data->all());
明确的学习思路能更高效的学习
点此加入该群学习
本文发布于:2023-04-07 20:33:06,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/75b572874230e264103454adcd2c1200.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:php使用QueryList轻松采集JavaScript动态渲染页面.doc
本文 PDF 下载地址:php使用QueryList轻松采集JavaScript动态渲染页面.pdf
| 留言与评论(共有 0 条评论) |