Setup a Static IP for Python Web Scraping using Selenium

Learn how to route Python Selenium web scraping traffic through a QuotaGuard Static IP proxy.

Prerequisites

N/A

Instructions

Run example
QUOTAGUARDSTATIC_URL=... bin/qgpass python app.py

Be sure to set QUOTAGUARDSTATIC_URL to your Connection URL from the QuotaGuard Dashboard.

Test in Docker
docker build -t qg-static-python-https-selenium-example .
docker run -e QUOTAGUARDSTATIC_URL=... qg-static-python-https-selenium-example

Code Samples

app.py
#/usr/bin/env python
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By

# Configure Selenium to use a local proxy
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument('--proxy-server=http://localhost:8080')  # Use localhost and port 8080 for qgpass

# Create a new instance of the Chrome driver
service = Service(executable_path='/usr/local/bin/chromedriver')
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options)

# Example: Open a website
driver.get("https://ip.quotaguard.com")

# Do something with the website
body_element = driver.find_element(By.TAG_NAME, 'body')
print(body_element.text)

# Clean up
driver.quit()

Docker File
FROM python:latest

WORKDIR /app/

# install google chrome
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
RUN sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list'
RUN apt-get -y update
RUN apt-get install -y google-chrome-stable

# Download and extract qgpass
RUN curl https://s3.amazonaws.com/quotaguard/qgpass-latest.tar.gz | tar xz -C .

# install selenium
RUN pip install selenium
RUN pip install webdriver-manager

# Copy the project files into the container
COPY . .

# Command to run when the container starts
CMD [ "bin/qgpass", "python", "app.py"]