场景:浏览网页
我有一个目标网站,我需要抓取并采取个人账户饲料的截图。
需求:
- 登录该网站。
- 浏览到个人区域。
- 抓取页面。
代码:
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);
你必须使用phantom-js?只有当你的网页有JS时,你才需要它。 –
@mortezakavakebi我的网页正在使用JS,所以我必须使用phantom-js – Faxsy