byn9826 / fake-ssr
此包已被弃用且不再维护。未建议替代包。
为JavaScript渲染页面提供渲染后的HTML内容供网络爬虫抓取
dev-master
2018-04-16 01:42 UTC
Requires
- jaybizzle/crawler-detect: ^1.2
This package is not auto-updated.
Last update: 2020-01-20 19:47:06 UTC
README
为JavaScript渲染页面/单页应用提供渲染后的HTML内容供网络爬虫抓取。
用户将正常访问页面,而网络爬虫将直接获取已渲染的HTML。
先决条件
安装Google Chrome
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
sudo dpkg -i google-chrome-stable_current_amd64.deb
sudo apt-get -f install
安装
composer require "byn9826/fake-ssr:dev-master"
用法
在默认控制器默认操作(在此处处理所有流量)。
use byn9826\FakeSSR\FakeSSR;
class IndexController extends ControllerBase {
public function indexAction() {
//$cache_folder is the location of the folder used to cache rendered HTML
//make sure www-data could execute in this folder
//$cache_folder could be null if $expire is 0
$cache_folder = dirname(__dir__) . '/.ssr';
//$expire is the cache expiring time.
//Default value is false, means never expire.
//0 means never use cache.
//1 means cache for 1 min, 10 means cache for 10 min, 100 means cache for 100 min, etc
$expire = 0;
FakeSSR::detect($cache_folder, $expire);
//Render the index.html for the single page application
include(dirname(__dir__) . '/frontend/index.html');
}
}