셀레늄 파이썬(4). Python 및 프록시와 함께 Selenium을 사용하는 방법
Python과 Selenium의 강력한 기능을 발견하고 효율적인 웹 스크래핑 및 자동화를 위해 프록시를 활용하세요. FineProxy의 저렴한 프록시 서버로 데이터 수집을 강화하세요.
Selenium 및 Python을 사용하여 웹 스크래핑 또는 자동화 작업을 수행할 때 IP 금지를 방지하고 데이터 수집 효율성을 높이려면 프록시를 사용하는 것이 필수적일 수 있습니다. 이 기사에서는 Python을 사용하여 Selenium에서 프록시를 설정하고 사용하는 방법을 안내합니다.
Selenium이란 무엇이고 프록시를 왜 사용할까?#
Selenium 웹 브라우저 자동화를 위한 강력한 도구로, 웹 스크래핑과 웹 애플리케이션 테스트에 자주 사용됩니다. 프록시 최종 사용자와 그들이 브라우징하는 웹사이트 사이의 중개 서버로, IP 주소를 마스킹하고 요청 로드를 관리하는 데 도움이 됩니다.
Selenium과 프록시 설정#
코드를 시작하기 전에 필요한 도구가 설치되어 있는지 확인하세요:
- Python
- Selenium (
pip install selenium) - 웹 브라우저 드라이버 (예: Chrome용 ChromeDriver 또는 Firefox용 GeckoDriver)
Selenium에서 프록시 사용 단계별 가이드#
1. 프록시로 Chrome 설정하기#
프록시 서버를 사용하도록 Chrome을 구성하려면 Selenium에서 원하는 기능을 설정해야 합니다.
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
# Specify proxy details
proxy = "your_proxy_ip:port"
# Set up Chrome options
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument(f'--proxy-server={proxy}')
# Initialize WebDriver
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options)
# Open a website to verify the proxy
driver.get("http://whatismyipaddress.com")
# Close the browser
driver.quit()
인증 방식 프록시#
프록시에 인증이 필요한 경우 다음을 사용할 수 있습니다 Proxy 클래스(다음 출처) selenium.webdriver.common.proxy.
from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy, ProxyType
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
# Proxy settings
proxy = Proxy()
proxy.proxy_type = ProxyType.MANUAL
proxy.http_proxy = "your_proxy_ip:port"
proxy.ssl_proxy = "your_proxy_ip:port"
proxy.add_argument('--proxy-auth=user:password')
capabilities = webdriver.DesiredCapabilities.CHROME
proxy.add_to_capabilities(capabilities)
# Initialize WebDriver
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), desired_capabilities=capabilities)
# Open a website to verify the proxy
driver.get("http://whatismyipaddress.com")
# Close the browser
driver.quit()
Firefox에서 프록시 사용하기#
Firefox가 프록시를 사용하도록 구성하려면 Firefox 프로필 설정을 수정하세요.
from selenium import webdriver
from selenium.webdriver.firefox.service import Service
from webdriver_manager.firefox import GeckoDriverManager
# Proxy details
proxy_ip = "your_proxy_ip"
proxy_port = "port"
# Set up Firefox profile
profile = webdriver.FirefoxProfile()
profile.set_preference("network.proxy.type", 1)
profile.set_preference("network.proxy.http", proxy_ip)
profile.set_preference("network.proxy.http_port", int(proxy_port))
profile.set_preference("network.proxy.ssl", proxy_ip)
profile.set_preference("network.proxy.ssl_port", int(proxy_port))
# Initialize WebDriver
driver = webdriver.Firefox(service=Service(GeckoDriverManager().install()), firefox_profile=profile)
# Open a website to verify the proxy
driver.get("http://whatismyipaddress.com")
# Close the browser
driver.quit()
일반적인 문제 해결#
프록시 인증: 프록시에 로그인이 필요한 경우 자격 증명이 올바르게 설정되었는지 확인하세요.
IP 차단: 일부 웹사이트에서는 여전히 프록시를 차단할 수 있습니다. 이 문제를 완화하려면 주거용 또는 순환 프록시를 사용하십시오.
요약#
Selenium 및 Python과 함께 프록시를 사용하면 IP 제한을 우회하고 웹 스크래핑 프로젝트를 향상시킬 수 있습니다. Chrome 또는 Firefox를 사용하든, 프록시 설정에는 브라우저 옵션 및 기능을 구성하여 트래픽을 프록시 서버를 통해 라우팅하는 것이 포함됩니다.
| 브라우저 | 설정 방법 | 인증 | 코드 스니펫 사용 가능 |
|---|---|---|---|
| Chrome | ChromeOptions | 예 | 예 |
| Firefox | Firefox프로필 | 예 | 예 |
이 단계를 따르면 스크래핑 작업이 중단 없이 효율적으로 진행될 수 있습니다. 질문이나 향후 튜토리얼에 대한 아이디어가 있으시면 편하게 댓글을 남겨주세요!