2017-03-17 173 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真的没有解决这个问题。 –