php+phantomjs獲取網頁渲染後的內容,使用jonnyw/php-phantomjs獲取網頁渲染後的內容

注意:本文是在windows2019系統下運行,本人親測有效,其他環境未進行驗證

2022.6.26 windows2022也進行了測試,要稍微多一點東西,本文也進行了說明

2022.7.15 測試了php8.0,一切正常

1、配置系統環境

新系統windows2019→安裝寶塔→安裝iis+php7.4→創建網站

2、安裝phantomjs

參考下面教程進行安裝

這裡需要記錄下phantomjs的安裝路徑,後續程序裡面需要用到

3、使用composer安裝jonnyw/php-phantomjs

①安裝composer

參考下面教程

②安裝jonnyw/php-phantomjs

特別注意:要使用composer進行安裝,需要進入php7.4刪除禁用函數putenv,不然會出現下面錯誤

php+phantomjs獲取網頁渲染後的內容,使用jonnyw/php-phantomjs獲取網頁渲染後的內容

參考網址:https://jonnnnyw.github.io/php-phantomjs/

a.打開Windows PowerShell,並進入到網站根目錄下

cd d:/wwwroot/ceshi

b.運行命令安裝jonnyw/php-phantomjs

注意:如果系統是windows2022,就需要去刪除禁用函數proc_get_status,不然會出現下面的錯誤

php+phantomjs獲取網頁渲染後的內容,使用jonnyw/php-phantomjs獲取網頁渲染後的內容

然後輸入下面的命令

composer require "jonnyw/php-phantomjs:4.*"

安裝成功如下

php+phantomjs獲取網頁渲染後的內容,使用jonnyw/php-phantomjs獲取網頁渲染後的內容

安裝成功後網站根目錄下面會多一個vendor文件夾,裡面包含的內容如下

php+phantomjs獲取網頁渲染後的內容,使用jonnyw/php-phantomjs獲取網頁渲染後的內容

4、測試jonnyw/php-phantomjs是否安裝成功

①在網站根目錄新建一個php文件,命名為ceshi.php,添加下面的代碼到php文件中

注意:這個時候需要去刪除禁用函數proc_open,不然會出錯,具體會出現的錯誤可以看後面

<?php

require 'vendor/autoload.php';
use JonnyW\PhantomJs\Client;

$client = Client::getInstance();
$client->getEngine()->setPath('D:\ruanjian\phantomjs-2.1.1-windows\bin\phantomjs.exe'); //這裡的路徑為你實際安裝phantomjs的位置,請根據需要修改

$client->getEngine()->addOption('--load-images=true'); //設為false表示禁止加載圖片
$client->getEngine()->addOption('--ignore-ssl-errors=true'); //忽略ssl錯誤
//具體設置,參考網址https://phantomjs.org/api/command-line.html進行添加

$link = 'https://www.binbinbiji.com/'; //需要請求渲染的網址

$request  = $client->getMessageFactory()->createRequest();
$response = $client->getMessageFactory()->createResponse();


//$request->addSetting('userAgent', 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36');//設置ua,上面和下面方法都行
$request->addHeader('user-agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36'); //設置UA,上面和下面方法都行
$request->addHeader('cookie', ''); //設置cookie

$client->isLazy(); // 讓客戶端等待所有資源加載完畢
$request->setTimeout(30000); // 設置超時時間(超過這個時間停止加載並渲染輸出畫面)

$request->setMethod('GET'); //設置請求方法
$request->setUrl($link); //設置請求連接
$client->send($request, $response); //發送請求獲取響應

//echo $response->isRedirect(); //網站是否有跳轉
//echo $response->getRedirectUrl(); //獲取跳轉後的網址

echo $response->getContent(); //輸出渲染後的源碼內容
/*
if($response->getStatus() === 200) {
    //輸出抓取內容
    echo $response->getContent();
    //獲取內容後的處理
}*/ //如果注釋掉上面的輸出,啟用下面的,就必須狀態碼為200的才行,就不能獲取到跳轉後的內容了。

②瀏覽器中打開網址,出現下面錯誤

php+phantomjs獲取網頁渲染後的內容,使用jonnyw/php-phantomjs獲取網頁渲染後的內容
Warning: proc_open() has been disabled for security reasons in D:\wwwroot\ceshi\vendor\jonnyw\php-phantomjs\src\JonnyW\PhantomJs\Procedure\Procedure.php on line 107

Fatal error: Uncaught JonnyW\PhantomJs\Exception\ProcedureFailedException: Error when executing PhantomJs procedure - proc_open() did not return a resource in D:\wwwroot\ceshi\vendor\jonnyw\php-phantomjs\src\JonnyW\PhantomJs\Procedure\Procedure.php:138 Stack trace: #0 D:\wwwroot\ceshi\vendor\jonnyw\php-phantomjs\src\JonnyW\PhantomJs\Procedure\ProcedureValidator.php(84): JonnyW\PhantomJs\Procedure\Procedure->run(Object(JonnyW\PhantomJs\Procedure\Input), Object(JonnyW\PhantomJs\Procedure\Output)) #1 D:\wwwroot\ceshi\vendor\jonnyw\php-phantomjs\src\JonnyW\PhantomJs\Procedure\ProcedureValidator.php(61): JonnyW\PhantomJs\Procedure\ProcedureValidator->validateSyntax('\n\n/**\n * Set up...') #2 D:\wwwroot\ceshi\vendor\jonnyw\php-phantomjs\src\JonnyW\PhantomJs\Procedure\ProcedureCompiler.php(107): JonnyW\PhantomJs\Procedure\ProcedureValidator->validate('\n\n/**\n * Set up...') #3 D:\wwwroot\ceshi\vendor\jonnyw\php-phantomjs\src\JonnyW\PhantomJs\Client.php(160): JonnyW\PhantomJs\Procedure\ProcedureCompiler->compile(Object(JonnyW\P in D:\wwwroot\ceshi\vendor\jonnyw\php-phantomjs\src\JonnyW\PhantomJs\Procedure\Procedure.php on line 138

意思是proc_open函數被禁用了,我們去php7.4裡面刪除禁用函數proc_open就行了

php+phantomjs獲取網頁渲染後的內容,使用jonnyw/php-phantomjs獲取網頁渲染後的內容

③再次打開網址,這個時候網站正常打開可以訪問了,安裝成功

php+phantomjs獲取網頁渲染後的內容,使用jonnyw/php-phantomjs獲取網頁渲染後的內容

到此為止。

發布者:彬彬筆記,轉載請註明出處:https://www.binbinbiji.com/zh-hant/php/2479.html

(2)
彬彬筆記彬彬筆記
上一篇 2022年6月21日 19:17
下一篇 2022年6月27日 13:33

相關推薦

發表回復

登錄後才能評論
蜀ICP備14017386號-13