您当前的位置:学无止境 > querylist无头百度搜索网站首页学无止境
querylist无头百度搜索
发布时间:2024-08-07 16:42:14编辑:tulouguli查看次数:1387
不建议用 linux
条件:
安装nodejs
安装puphpeteer(npm install @nesk/puphpeteer)
下载库jaeger/querylist和jaeger/querylist-puppeteer(建议用composer)
proc_open函数没有被禁用
<?php
require ('./vendor/autoload.php');
use QLQueryList;
use QLExtChrome;
$ql = QueryList::getInstance();
// 注册插件,默认注册的方法名为: chrome
$ql->use(Chrome::class);
$ql->chrome(function ($page,$browser) {
$page->goto('https://www.baidu.com/');
// 这里故意设置一个很长的延长时间,让你可以看到chrome浏览器的启动
sleep(3);
//输入关键词
$wd = 'lovesanqing';
$page->type("input[id='kw']",$wd);
sleep(1);
//点击搜索
$page->click("input[type='submit']");
//等待搜索结果
sleep(3);
//获取结果
$html = $page->content();
//用jquery选择器抽取结果
$rules = array(
'title'=>['#content_left h3 a','text'],//标题
'url'=>['#content_left h3 a','href'],//跳转网址
'description'=>['div .c-abstract','text'],//描述
'img'=>['#content_left .general_image_pic a img','src'],//搜索列表图片
);
$ql = QueryList::html($html);
$rt = $ql->rules($rules)->query()->getData();
print_r($rt->all());
//如果有需要,可以把$rt入库,以及做其他操作
foreach ($rt as $key=>$val){
if($key == 'img'){ # 保存图片
$res = file_get_contents($val);
// 固定保存到指定文件夹中
$info = './images/'.time().rand(1,999999).'.jpg';
file_put_contents($info,$res);
}
}
// $page->focus('input[placeholder="验证码"]'); 鼠标聚焦事件
// 截图
$src_path = './images/'.time().rand(1,999999).'.jpg';
$page->screenshot([
'path' => $src_path,
'clip'=>[
'x'=>900,'y'=>4470, 'width'=>125,'height'=>40
]
]);
sleep(10);
$browser->close();
// 返回值一定要是页面的HTML内容
return $html;
},[
'headless' => true, // 启动可视化Chrome浏览器,方便调试(调试时可设置false)
'devtools' => false, // 打开浏览器的开发者工具
])->find('title')->text();来源:https://blog.csdn.net/tulouguli/article/details/104677594
关键字词:无头,headless,抓取,爬虫,QueryList,puppeteer
评论:
