PINGDOM_CHECK

How to use Selenium with Zyte Smart Proxy Manager

Posted on

6 Mins

Read Time

June 7, 2022

Categories
Using Selenium and/or Zyte Smart Proxy Manager? You just stumbled upon the right blog.

By

Neha Setia Nagpal

Return to top

How to use Selenium with Zyte Smart Proxy Manager

Introducing a new, easy-to-use library to work with Zyte Smart Proxy Manager

Using Selenium and/or Zyte Smart Proxy Manager? You just stumbled upon the right blog.

I am thrilled to share some good news with all the Selenium users, who are looking for an easy-to-integrate anti-ban solution, and all the Zyte Smart Proxy Manager users, who use Selenium (a Web Automation-Headless browser library) for extracting data from javascript-heavy websites. We have just launched a new Zyte SmartProxy Selenium

At Zyte, the developer experience matters the most, and we wanted to give you a smooth experience of scraping dynamic websites with seamless integration between Selenium and our smart rotating proxy service, Zyte Smart Proxy Manager.

Let’s dive into how to get started, it’s super easy!

What is Zyte SmartProxy Selenium library? 

Zyte SmartProxy Selenium library is a client library built on top of Selenium — an open-source framework for web automation across Chromium, Firefox, and WebKit, with a single API, written to work seamlessly with Zyte Smart Proxy Manager.

With this library, you will be able to make the best of the headless browser capabilities of Selenium and manage bans by unlocking the powerful proxy management tool - Zyte Smart Proxy Manager in your web scraping projects.

Using our library for Selenium you will no longer have to maintain a separate piece of software running in the background to help connect with Zyte Smart Proxy Manager. 

In this tutorial, I will demonstrate how your Selenium  web scraping script will have superhero capabilities to 

  • Handle data nested in scripts
  • Manage proxy rotation
  • Manage bans
  • Manage sessions
  • Increase cost savings in your project
  • Speed up page loads 

Prerequisite for the tutorial: 

In order to run the script used in the tutorial, please make sure that you are ready with the following:

  • Have a Zyte Smart Proxy Manager account
    If you don’t have an SPM account, sign-up for a 14-day free trial here. Check out our docs on subscribing to Smart Proxy Manager for detailed instructions.
  • Be ready with a Zyte Smart Proxy Manager API Key
    Once you sign up, you will see your API key on the Getting Started page.
    If you already have a Smart Proxy Manager subscription, select ‘Smart Proxy Manager’ under ‘Tools’ on the left side of the dashboard and click on ‘API Access’. Make note of it, as we will need it later in the code.
selenium
smart proxy manager

Setting up the Zyte SmartProxy Selenium   

Before installing the library, You first need to install browser drivers, for the browser you want to work with. We will be using Chromium/Chrome for this tutorial, so you need to download the  ‘ChromeDriver’, as per your current version of the Chrome browser. To know the current version of the chrome, 

  1. Open your browser
  2. At the top right, look at More (Vertical Ellipsis-three vertical dots)
  3. Click Help > About Chrome
  4. Now from this page, download the driver with the version that matches your chrome version
  5. Update $PATH environment variable using these instructions

Now, let’s install the Zyte SmartProxy Selenium library. Just run the following command using ‘pip’, and it will install all the dependencies along with the native selenium library.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$ python3 -m pip install zyte-smartproxy-selenium
$ python3 -m pip install zyte-smartproxy-selenium
$ python3 -m pip install zyte-smartproxy-selenium

Awesome, now that you are all set and configured. Let’s code!

Using Zyte SmartProxy Selenium for Seamless web scraping

To demonstrate the integration between Zyte Smart Proxy Manager and Headless browser library - Selenium, we will write a script that will cause our headless browser to take a screenshot of ‘Web Scraping Sandbox’. This sandbox is developed by Zyte for demonstration purposes, feel free to play around with it and experiment with new techniques around web scraping. 

Let’s start our Zyte SmartProxy Selenium tutorial with this basic example. 

Create a new file with the name sample.py and open it in your favorite code editor

  1. First, let’s import the Zyte SmartProxy Selenium library with the ‘webdriver’ class in your script.
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
from zyte_smartproxy_selenium import webdriver
from zyte_smartproxy_selenium import webdriver
from zyte_smartproxy_selenium import webdriver
  1. Next, create an instance of the browser with additional parameters - spm_apikey
    Set the value of spm_apikey, as mentioned in the prerequisite above.
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
browser = webdriver.Chrome(spm_options={'spm_apikey': '<Smart Proxy Manager API KEY>'})
browser = webdriver.Chrome(spm_options={'spm_apikey': '<Smart Proxy Manager API KEY>'})
browser = webdriver.Chrome(spm_options={'spm_apikey': '<Smart Proxy Manager API KEY>'})
  1. Now, you can request for any webpage to be loaded, with the help of get function. 
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
browser.get('https://toscrape.com')
browser.get('https://toscrape.com')
browser.get('https://toscrape.com')
  1. Take a screenshot of the web scraping sandbox and save it, with save_screenshot command. In the path argument, give the path to the directory where you want to save the screenshot. The path used in this script will save the screenshot in your current directory which contains sample.py.
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
browser.save_screenshot('screenshot.png')
browser.save_screenshot('screenshot.png')
browser.save_screenshot('screenshot.png')
  1. Finally, don’t forget to close the browser.
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
browser.close()
browser.close()
browser.close()

The final code should look like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
from zyte_smartproxy_selenium import webdriver
browser = webdriver.Chrome(spm_options={'spm_apikey': '<Smart Proxy Manager API KEY>'})
browser.get('https://toscrape.com')
browser.save_screenshot('screenshot.png')
browser.close()
from zyte_smartproxy_selenium import webdriver browser = webdriver.Chrome(spm_options={'spm_apikey': '<Smart Proxy Manager API KEY>'}) browser.get('https://toscrape.com') browser.save_screenshot('screenshot.png') browser.close()
from zyte_smartproxy_selenium import webdriver

browser = webdriver.Chrome(spm_options={'spm_apikey': '<Smart Proxy Manager API KEY>'})
browser.get('https://toscrape.com')
browser.save_screenshot('screenshot.png')
browser.close()

Execute script on the command line.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$ python3 sample.py
$ python3 sample.py
$ python3 sample.py

If your script runs successfully, You should be able to see screenshot.png in your project folder.

Additional functionalities 

In addition to easy integration and management of headless capabilities of Selenium with Zyte Smart Proxy Manager, our library provides additional functionalities such as

  • Automatic session management with Zyte Smart Proxy Manager.
  • Ad-blocking to speed up page loads and save costs (Zyte Smart Proxy Manager charges only per successful request). You can use the 'block_ads' argument and set it 'true'. and the library will block ads defined by block_list.
  • Direct downloading static assets (images, CSS, javascript, etc.) to speed up page load and save costs in Zyte Smart Proxy requests. You can use the 'static_bypass' argument and set it to 'true'. and the library will skip the Proxy used for static assets defined by 'static_bypass_regex' or pass false to use the Proxy.

Important note: block_ads and static_bypass are enabled by default. Some websites may not work with block_ads and static_bypass enabled. Try disabling them if you encounter any issues. To know more about these functionalities, read here

Troubleshooting

Error: 

You may encounter the following exception

Resolution:

Double-check the path to the ChromeDriver, you can set the path as shown in the screenshot. This screenshot follows these instructions.

 

Learn how to superpower your Smart Proxy Manager with Selenium

Using libraries like Zyte SmartProxy Selenium can make it so much easier to work with and can make it so much easier to work with dynamic websites and manage bans and proxies all together in a single piece of code. Later this month, on the 22nd of June, I will be hosting a webinar to demonstrate the true power of this new integration and show you how to make the most out of it. So be sure to join me!

This webinar will be a good opportunity for you to interact with our web scraping experts and clarify your doubts on the fly while doing hands-on integration of these libraries.

Get successful requests with Smart Proxy Manager

Read more

If you are new to headless browsers, Selenium and Zyte Smart Proxy Manager. Here are some links to learn more about these topics. I hope you find them useful. 

  • Zyte Smartproxy Selenium: A wrapper over Selenium Wire to provide Zyte Smart Proxy Manager specific functionalities.
  • Zyte Smart Proxy Manager: When extracting web data at scale using proxy management is critical to avoid getting banned or blocked. Smart Proxy Manager automatically selects the best proxies to keep your crawl healthy. It handles retries and applies rotation and fingerprinting logic to maximize your success rate.
  • Integration SPM with Selenium: Learn more about the integration on our company docs.
  • What is a Headless Browser?: A headless browser is a web browser without a user interface. Basically, it’s the same Chrome or Firefox we normally use with things we can click or touch stripped away: no tab bar, URL bar, bookmarks, or any other elements for visual interaction. 
  • How does a headless browser help with web scraping and data extraction?: To understand the role of headless browsers in web scraping with our Technical Lead-Pawel.
  • Selenium: Selenium is an open-source umbrella project for a range of tools and libraries aimed at supporting browser automation. It provides a playback tool for authoring functional tests without the need to learn a test scripting language.
selenium
smart proxy manager