12.Using Proxies With Python Selenium _ ScrapeOps
12.Using Proxies With Python Selenium _ ScrapeOps
In this guide for The Python Selenium Web Scraping Playbook, we will look at how to integrate proxies
into our Python Selenium based web scraper.
There are number of different types of proxies which you need to integrate differently with Selenium, so
we will walk through how to integrate each type:
https://scrapeops.io/selenium-web-scraping-playbook/python-selenium-proxy/ 1/6
3/31/24, 9:33 PM Using Proxies With Python Selenium | ScrapeOps
"11.456.448.110:8080"
Depending on which type of browser you are using the integration method is slightly different.
## Example Proxy
PROXY = "11.456.448.110:8080"
Now when we run the script we can see that Selenium is using the defined proxy IP:
{
"origin": "11.456.448.110:8080"
}
## Define Proxy
proxy = Proxy({
'proxyType': ProxyType.MANUAL,
'httpProxy': "11.456.448.110:8080",
https://scrapeops.io/selenium-web-scraping-playbook/python-selenium-proxy/ 2/6
3/31/24, 9:33 PM Using Proxies With Python Selenium | ScrapeOps
'noProxy': ''
})
## Create Driver
firefox_driver = webdriver.Firefox(proxy = proxy, executable_path=r"/root/geckodriver")
Now when we run the script we can see that Selenium is using the defined proxy IP:
{
"origin": "11.456.448.110:8080"
}
This method works fine when you don't need to add an authentication username and password to
the proxy. We will look at how to use authenticated proxies in another section.
It is very common for commercial proxy providers to sell access to their proxy pools by giving you single
proxy endpoint that you send your requests too and authenticate your account using a username and
password .
"http://USERNAME:PASSWORD@proxy-server:8080"
There are a couple ways to solve this, but one of the easiest is to use the Selenium Wire extension which
makes it very easy to use proxies with Selenium.
https://scrapeops.io/selenium-web-scraping-playbook/python-selenium-proxy/ 3/6
3/31/24, 9:33 PM Using Proxies With Python Selenium | ScrapeOps
Then update your scraper to use the seleniumwire webdriver instead of the default selenium
webdriver :
Now when we run the script we can see that Selenium is using a proxy IP:
{
"origin": "201.88.548.330:8080"
}
Selenium Wire has a lot of other powerful functionality, so if you would like to learn more then check out
our full Selenium Wire guide here.
However, these proxy API endpoints don't integrate well with headless browsers when the website is
using relative links as Selenium will try to attach the relative URL onto the proxy API endpoint not the
websites root URL. Resulting, in some pages not loading correctly.
https://scrapeops.io/selenium-web-scraping-playbook/python-selenium-proxy/ 4/6
3/31/24, 9:33 PM Using Proxies With Python Selenium | ScrapeOps
As a result, when integrating your Selenium scrapers it is recommended that you use their proxy port
integration over the API endpoint integration when they provide them (not all do have a proxy port
integration).
For example, in the case of the ScrapeOps Proxy Aggregator we offer a proxy port integration for
situations like this.
The proxy port integration is a light front-end for the API and has all the same functionality and
performance as sending requests to the API endpoint but allow you to integrate our proxy aggregator
as you would with any normal proxy.
The following is an example of how to integrate the ScrapeOps Proxy Aggregator into your Selenium
scraper using
SCRAPEOPS_API_KEY = 'APIKEY'
Full integration docs for Python Selenium and the ScrapeOps Proxy Aggregator can be found here.
TIP
To use the ScrapeOps Proxy Aggregator, you first need an API key which you can get by signing up
for a free account here which gives you 1,000 free API credits.
https://scrapeops.io/selenium-web-scraping-playbook/python-selenium-proxy/ 5/6
3/31/24, 9:33 PM Using Proxies With Python Selenium | ScrapeOps
If you would like to learn more about Web Scraping with Selenium, then be sure to check out The
Selenium Web Scraping Playbook.
https://scrapeops.io/selenium-web-scraping-playbook/python-selenium-proxy/ 6/6