PHP code example of jaeger / querylist-puppeteer
1. Go to this page and download the library: Download jaeger/querylist-puppeteer library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
jaeger / querylist-puppeteer example snippets
use QL\QueryList;
use QL\Ext\Chrome;
$ql = QueryList::getInstance();
// 注册插件,默认注册的方法名为: chrome
$ql->use(Chrome::class);
// 或者自定义注册的方法名
$ql->use(Chrome::class,'chrome');
// 抓取的目标页面是使用Vue.js动态渲染的页面
$text = $ql->chrome('https://www.iviewui.com/components/button')->find('h1')->text();
print_r($text);
// 输出: Button 按钮
$rules = [
'h1' => ['h1','text']
];
$ql = $ql->chrome('https://www.iviewui.com/components/button');
$data = $ql->rules($rules)->queryData();
$text = $ql->chrome('https://www.iviewui.com/components/button',[
'timeout' => 6000,
'ignoreHTTPSErrors' => true,
// ...
])->find('h1')->text();
$text = $ql->chrome(function ($page,$browser) {
$page->setUserAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36');
// 设置cookie
$page->setCookie([
'name' => 'foo',
'value' => 'xxx',
'url' => 'https://www.iviewui.com'
],[
'name' => 'foo2',
'value' => 'yyy',
'url' => 'https://www.iviewui.com'
]);
$page->goto('https://www.iviewui.com/components/button');
// 等待h1元素出现
$page->waitFor('h1');
// 获取页面HTML内容
$html = $page->content();
// 关闭浏览器
$browser->close();
// 返回值一定要是页面的HTML内容
return $html;
})->find('h1')->text();
$text = $ql->chrome(function ($page,$browser) {
$page->goto('https://www.iviewui.com/components/button');
// 页面截图
$page->screenshot([
'path' => 'page.png',
'fullPage' => true
]);
$html = $page->content();
$browser->close();
return $html;
})->find('h1')->text();
$text = $ql->chrome(function ($page,$browser) {
$page->goto('https://www.iviewui.com/components/button');
$html = $page->content();
// 这里故意设置一个很长的延长时间,让你可以看到chrome浏览器的启动
sleep(10000000);
$browser->close();
// 返回值一定要是页面的HTML内容
return $html;
},[
'headless' => false, // 启动可视化Chrome浏览器,方便调试
'devtools' => true, // 打开浏览器的开发者工具
])->find('h1')->text();