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.
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,Â
- Open your browser
- At the top right, look at More (Vertical Ellipsis-three vertical dots)
- Click Help > About Chrome
- Now from this page, download the driver with the version that matches your chrome version
- 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.
$ 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
- First, let’s import the Zyte SmartProxy Selenium library with the ‘webdriver’ class in your script.
from zyte_smartproxy_selenium import webdriver
- Next, create an instance of the browser with additional parameters -
spm_apikey
.Â
Set the value ofspm_apikey
, as mentioned in the prerequisite above.
browser = webdriver.Chrome(spm_options={'spm_apikey': '<Smart Proxy Manager API KEY>'})
- Now, you can request for any webpage to be loaded, with the help of
get
function.Â
browser.get('https://toscrape.com')
- 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.
browser.save_screenshot('screenshot.png')
- Finally, don’t forget to close the browser.
browser.close()
The final code should look like this:
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.
$ 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 byblock_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
argument and set it to'
static_bypass'
'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.