Selenium ile Proxy Kullanımı (Chrome ve Firefox)

18 views

Selenium ile Proxy Kullanımı

Selenium, web tarayıcı otomasyonu için en popüler araçlardan biridir. Web scraping, test otomasyonu ve bot geliştirme gibi projelerde proxy kullanımı genellikle zorunludur. Bu makalede Chrome ve Firefox için proxy ayarlamayı adım adım anlatacağız.

1. Gerekli Kütüphanelerin Yüklenmesi

Bash

pip install selenium webdriver-manager

Not: webdriver-manager kütüphanesi, driver indirme işlemini otomatikleştirir.

2. Google Chrome ile Proxy Kullanımı

Temel Proxy Ayarı (HTTP/HTTPS)

Python

from selenium import webdriver from selenium.webdriver.chrome.options import Options from webdriver_manager.chrome import ChromeDriverManager from selenium.webdriver.chrome.service import Service chrome_options = Options() chrome_options.add_argument('--proxy-server=http://proxy_ip:proxy_port') driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options) driver.get("https://httpbin.org/ip") print(driver.page_source) driver.quit()

Proxy Kimlik Doğrulama ile Kullanım

Chrome’da kimlik doğrulama için Proxy Auth eklentisi veya  DesiredCapabilities yöntemi kullanılır. En temiz yöntem Proxy Auth eklentisi kullanmaktır:

Python

from selenium import webdriver from selenium.webdriver.chrome.options import Options from webdriver_manager.chrome import ChromeDriverManager from selenium.webdriver.chrome.service import Service PROXY = "kullanici_adi:sifre@proxy_ip:proxy_port" chrome_options = Options() chrome_options.add_argument(f'--proxy-server=http://{PROXY}') driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options) driver.get("https://httpbin.org/ip") driver.quit()

3. Mozilla Firefox ile Proxy Kullanımı

Firefox, proxy ayarlarını daha esnek yönetir.

Temel Proxy Ayarı

Python

from selenium import webdriver from selenium.webdriver.firefox.options import Options from webdriver_manager.firefox import GeckoDriverManager from selenium.webdriver.firefox.service import Service firefox_options = Options() firefox_options.set_preference("network.proxy.type", 1) firefox_options.set_preference("network.proxy.http", "proxy_ip") firefox_options.set_preference("network.proxy.http_port", proxy_port) firefox_options.set_preference("network.proxy.ssl", "proxy_ip") firefox_options.set_preference("network.proxy.ssl_port", proxy_port) driver = webdriver.Firefox(service=Service(GeckoDriverManager().install()), options=firefox_options) driver.get("https://httpbin.org/ip") driver.quit()

SOCKS5 Proxy Kullanımı (Firefox)

Python

firefox_options.set_preference("network.proxy.type", 1) firefox_options.set_preference("network.proxy.socks", "proxy_ip") firefox_options.set_preference("network.proxy.socks_port", proxy_port) firefox_options.set_preference("network.proxy.socks_version", 5) firefox_options.set_preference("network.proxy.socks_remote_dns", True)  # DNS sızıntısını önler

4. Tam Fonksiyonel Örnek (Chrome)

Python

from selenium import webdriver from selenium.webdriver.chrome.options import Options from webdriver_manager.chrome import ChromeDriverManager from selenium.webdriver.chrome.service import Service def create_chrome_with_proxy(proxy_ip, proxy_port, username=None, password=None):    chrome_options = Options()        if username and password:        proxy = f"http://{username}:{password}@{proxy_ip}:{proxy_port}"    else:        proxy = f"http://{proxy_ip}:{proxy_port}"        chrome_options.add_argument(f"--proxy-server={proxy}")    chrome_options.add_argument("--disable-blink-features=AutomationControlled")        driver = webdriver.Chrome(        service=Service(ChromeDriverManager().install()),        options=chrome_options    )    return driver # Kullanım driver = create_chrome_with_proxy("185.XX.XX.XX", 8080) driver.get("https://httpbin.org/ip") print(driver.page_source) driver.quit()

5. En İyi Uygulamalar

  • Her zaman webdriver-manager kullanın (driver güncellemelerini otomatik yapar).
  • Proxy kimlik doğrulama için Proxy Auth eklentisi veya selenium-wire kütüphanesini tercih edin.
  • disable-blink-features=AutomationControlled argümanı ile bot tespitini azaltın.
  • Proxy rotasyonu yapıyorsanız her yeni istekte yeni driver oluşturun.
  • Hata yönetimi için try-except blokları kullanın.

6. Sık Karşılaşılan Sorunlar

Sorun Çözüm
Proxy bağlanmıyor Proxy IP ve portu kontrol edin
Kimlik doğrulama çalışmıyor selenium-wire kütüphanesini kullanın
Bot tespit ediliyor disable-blink-features ve User-Agent değiştirin
SOCKS5 proxy çalışmıyor Firefox’ta socks_remote_dns ayarını aktif edin

Sonuç

Selenium ile proxy kullanımı oldukça esnektir. Chrome ve Firefox için farklı yöntemler olsa da temel mantık aynıdır. Proxy kimlik doğrulama ve bot tespitini azaltma teknikleri ile daha stabil otomasyonlar geliştirebilirsiniz.

Bir sonraki makalemizde Scrapy ile Proxy Kullanımı konusunu detaylı olarak inceleyeceğiz.

Hangi proxy türünün sizin projenize daha uygun olduğunu öğrenmek isterseniz, bizimle iletişime geçebilirsiniz.

 

ProxymoTR - Premium Proxy Services

Dijital Kartvizit

Top