How to use Selenium for web scraping and use proxies for privacy

SwiftProxy
By - Linh Tran
2025-01-15 20:30:11

Selenium is a powerful tool that allows users to scrape data by simulating user operations in the browser, such as clicking, filling out forms, submitting, etc. When performing web crawlers, using a proxy can hide your real IP address and prevent the target website from being blocked due to frequent visits. This article will detail how to use Selenium for web scraping and combine it with a proxy to protect privacy.

Environment Setup

First, you need to make sure that Python and Selenium libraries are installed on your computer. You can install the Selenium library using the following command:

pip install selenium

Next, download the corresponding browser driver (such as ChromeDriver, GeckoDriver, etc.) according to your browser type (such as Chrome, Firefox, etc.) and add it to the system's PATH environment variable.

Web scraping using Selenium

Here is a simple example of scraping web data using Selenium:


from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options

# Configure ChromeDriver Path
chrome_driver_path = 'path/to/chromedriver'  # Replace with your ChromeDriver path

# Initialize ChromeDriver Service
service = Service(chrome_driver_path)

# Related configuration when opening the browser
options = Options()
options.add_argument("--start-maximized")  # Maximize window on startup

# Initializing WebDriver
driver = webdriver.Chrome(service=service, options=options)

# Open the target web page
driver.get('https://example.com')

# Locate elements and grab data
elements = driver.find_elements_by_xpath("//div[@class='example']")  # Use XPath to locate elements and modify them according to actual conditions
for element in elements:
    data = element.text
    print(data)  # Or save the data to a file, database, etc.

# Close the browser
driver.quit()

Combine with proxy to protect privacy

When crawling the web, using a proxy can hide your real IP address and prevent the target website from being blocked due to frequent visits. Here are the steps to use a proxy in Selenium:

‌1. Get the proxy server address and port‌

You can get the proxy server address and port by purchasing or using a free proxy service. (Considering factors such as security and speed, it is not recommended to use a free proxy)

‌2. Configure ChromeDriver to use a proxy‌

In Selenium, you can set the proxy by modifying the ChromeDriver startup parameters. The following is a sample code:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options

# Configure ChromeDriver path
chrome_driver_path = 'path/to/chromedriver' # Replace with your ChromeDriver path

# Initialize ChromeDriver Service
service = Service(chrome_driver_path)

# Related configuration when opening the browser
options = Options()
options.add_argument("--start-maximized") # Maximize the window when starting

# Set up a proxy
proxy_server = "http://proxy_server_address:port" # Replace with your proxy server address and port
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server=%s' % proxy_server)

# Initialize WebDriver
driver = webdriver.Chrome(service=service, options=options)

# Open the target webpage
driver.get('https://example.com')

# Locate elements and grab data
elements = driver.find_elements_by_xpath("//div[@class='example']") # Use XPath to locate elements, which can be modified according to actual conditions
for element in elements:
data = element.text
print(data) # Or save the data to a file, database, etc.

# Close the browser
driver.quit()

Notes

  • ‌Proxy availability‌: Make sure the proxy server you use is available and stable, otherwise it may affect the effect of web crawling.
  • ‌Anti-crawler mechanism‌: Many websites use anti-crawler mechanisms, such as verification codes, IP blocking, etc. When using Selenium for web crawling, you need to be careful to avoid triggering these mechanisms.
  • ‌Compliance‌: When performing web crawling, you need to comply with the terms of use and laws and regulations of the target website and must not use it for illegal purposes.

Note sur l'auteur

SwiftProxy
Linh Tran
Linh Tran est une rédactrice technique basée à Hong Kong, avec une formation en informatique et plus de huit ans d'expérience dans le domaine des infrastructures numériques. Chez Swiftproxy, elle se spécialise dans la simplification des technologies proxy complexes, offrant des analyses claires et exploitables aux entreprises naviguant dans le paysage des données en rapide évolution en Asie et au-delà.
Analyste technologique senior chez Swiftproxy
Le contenu fourni sur le blog Swiftproxy est destiné uniquement à des fins d'information et est présenté sans aucune garantie. Swiftproxy ne garantit pas l'exactitude, l'exhaustivité ou la conformité légale des informations contenues, ni n'assume de responsabilité pour le contenu des sites tiers référencés dans le blog. Avant d'engager toute activité de scraping web ou de collecte automatisée de données, il est fortement conseillé aux lecteurs de consulter un conseiller juridique qualifié et de revoir les conditions d'utilisation applicables du site cible. Dans certains cas, une autorisation explicite ou un permis de scraping peut être requis.
Join SwiftProxy Discord community Chat with SwiftProxy support via WhatsApp Chat with SwiftProxy support via Telegram
Chat with SwiftProxy support via Email