2017-03-17 235 views
3

場景:瀏覽網頁

我有一個目標網站,我需要抓取並採取個人賬戶飼料的截圖。

需求:

  1. 登錄該網站。
  2. 瀏覽到個人區域。
  3. 抓取頁面。

代碼:

require 'vendor/autoload.php'; 

use JonnyW\PhantomJs\Client; 

    $client = Client::getInstance(); 
    $client->getEngine()->setPath('C:\xampp\htdocs\phantomjs\bin\phantomjs.exe'); 
    $client->getProcedureCompiler()->clearCache(); 
    $client->isLazy(); 
    $delay = 15; // 5 seconds 
    $width = 1366; 
    $height = 768; 
    $top = 0; 
    $left = 0; 


    $request = $client->getMessageFactory()->createCaptureRequest(); 
    $response = $client->getMessageFactory()->createResponse(); 
    $request->setDelay($delay); 
    $request->setTimeout(10000); 


    $data = array(
    'login' => '***', 
    'password' => '***', 
    ); 

    $request->setMethod('POST'); 
    $request->setUrl('login-url'); 
    $request->setRequestData($data); // Set post data 
    $request->setOutputFile('screenshot.jpg'); 
    $request->setViewportSize($width, $height); 
    $request->setCaptureDimensions($width, $height, $top, $left); 

    $client->send($request, $response); 

    $file = fopen("1.txt","a"); 
    fwrite($file,$response->getContent()); 
    fclose($file); 

問:

如何瀏覽到個人頁面URL不會丟失cookie和會話?

我已經試圖只在相同的請求上再次更改setUrl,但它不起作用。

$request->setMethod('GET'); 
    $request->setUrl('personal-page-url'); 
    $request->setOutputFile('screenshot1.jpg'); 

    $client->send($request, $response); 

    $file = fopen("2.txt","a"); 
    fwrite($file,$response->getContent()); 
    fclose($file); 
+0

你必須使用phantom-js?只有當你的網頁有JS時,你才需要它。 –

+0

@mortezakavakebi我的網頁正在使用JS,所以我必須使用phantom-js – Faxsy

回答

1

根據這一issue on github ,還有不固定的問題與cookies。你可以關注它。

餅乾和php-phantomjs#124打開lucl22 10月1日, 2016開這個問題·3條評論

或者你可以使用報廢的其他方式,如果你的目標網頁沒有那麼多AJAX數據傳輸等:


,如果你真的需要JS來運行,你可以使用其他網絡驅動PHP

+0

這個問題不是關於瀏覽同一個cookie,而是關於cookie管理,而我的目標網站正在使用js – Faxsy

+0

添加了一些替換。因爲phantom-js真的沒有解決這個問題。 –