John Davidson

php - Set up one instance of ChromeDriver for Panther, rather than creating one for each request

0 comments
Message:


I am using Symfony Panther for web scraping (not testing) in a PHP project that does not use Symfony. I installed via Composer. Each time I need to scrape a link submitted by a user, I start a new Chrome Browser.


$client = Symfony\Component\Panther\Client::createChromeClient('/usr/bin/chromedriver');
$client->request('GET', $url);
$crawler = $client->waitFor('body');

Starting a new Chrome browser for each submitted $url is slow and resource intensive, so I want to keep the Chrome Client running on port 9515 and then each user's $url request can connect to that one same instance. Based on some user comments on Github, it sounds like a reasonable method:



  • Spin up a Chrome instance on the Linux server, running on port 9515

  • Make each url request connect to that instance.


I placed the first line i.e. with createChromeClient in a php script for a CRON job, but it never starts up the chrome client, and I get no errors either. Any ideas how to achieve this?



My Answer:
>get('https://example.com');

This code snippet creates a new instance of ChromeDriver every time a link is scraped. To optimize performance and resources, you can set up one instance of ChromeDriver for Panther and reuse it for multiple requests. Here's how you can do it:

php
// Create a global variable to store the Panther client
global $client;

// Check if the Panther client has been initialized
if (!$client) {
// Initialize the Panther client with ChromeDriver
$client = Symfony\Component\Panther\Client::createChromeClient('/usr/bin/chromedriver');
}

// Use the existing Panther client for scraping
$client->get('https://example.com');


By using this approach, you only create one instance of ChromeDriver for Panther and reuse it for all scraping requests. This can help improve performance and reduce resource usage.

Rate this post

4 of 5 based on 7500 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us