이 가이드는 제한을 우회하고 Web스크레이핑 성공률을 높이기 위해, 인증된 프록시와 함께 SeleniumBase를 설정하는 방법을 설명합니다.
Selenium은 인증된 프록시를 지원하지 않습니다. 이제 SeleniumWire는 1년 이상 전부터 deprecated 상태이며 2년 넘게 업데이트도 없으므로, 해결책은 SeleniumBase를 사용하는 것입니다. 후자의 프로젝트는 원래 Python으로 Web 자동화 및 테스트를 수행하기 위해 Selenium 인스턴스를 실행하는 래퍼로 설계되었습니다. 하지만 저희에게는 인증된 프록시를 사용하여 Selenium을 실행할 수 있게 해준다는 점이 핵심입니다.
먼저 SeleniumBase를 설치합니다:
pip install seleniumbase이제 Selenium을 제어하고 webdriver 인스턴스를 실행할 테스트 케이스를 작성합니다. 아래 코드는 IPinfo API로 요청를 보냅니다. 스크립트가 JSON 응답를 받으면, 응답를 파싱하고 그 내용을 콘솔에 출력합니다.
from seleniumbase import BaseCase
from selenium.webdriver.common.by import By
import json
class ProxyTest(BaseCase):
def test_proxy(self):
#go to the site
self.driver.get("https://ipinfo.io/json")
#load the json response
location_info = json.loads(self.driver.find_element(By.TAG_NAME, "body").text)
#iterate through the dict and print its contents
for k,v in location_info.items():
print(f"{k}: {v}")python이 아니라 pytest로 테스트를 실행해 보겠습니다.
프록시 없이 테스트 스크립트를 실행하려면 아래 명령을 사용합니다:
pytest proxy_test.py -s응답는 다음과 유사합니다:
=================================================== test session starts ===================================================
platform linux -- Python 3.10.12, pytest-8.3.4, pluggy-1.5.0
rootdir: /home/nultinator/clients/bright-data/seleniumbase-proxies
plugins: html-4.0.2, metadata-3.1.1, anyio-4.0.0, seleniumbase-4.34.6, ordering-0.6, rerunfailures-15.0, xdist-3.6.1
collected 1 item
proxy_test.py ip: 23.28.108.255
hostname: d28-23-255-108.dim.wideopenwest.com
city: Westland
region: Michigan
country: US
loc: 42.3242,-83.4002
org: AS12083 WideOpenWest Finance LLC
postal: 48185
timezone: America/Detroit
readme: https://ipinfo.io/missingauth
.
==================================================== 1 passed in 1.01s ====================================================
--proxy 플래그 뒤에 프록시 URL을 지정합니다. 템플릿은 다음과 같습니다:
--proxy=your_proxy_url:your_proxy_port
다음 예시는 Bright Data의 free proxy를 사용합니다. IP 주소는 155.54.239.64이며, 포트 80으로 통신합니다.
--proxy=155.54.239.64:80
인증된 프록시도 동일한 방식으로 처리됩니다. URL에 username과 password를 포함하기만 하면 됩니다:
proxy=<YOUR_USERNAME>:<YOUR_PASSWORD>@<PROXY_URL>:<PROXY_PORT>
가장 좋은 인증된 프록시 옵션은 다음과 같습니다:
- レジデンシャル프록시: 실제 사용자 IP를 사용하므로 봇 탐지 우회에 이상적입니다.
- データセンター프록시: 더 빠르고 비용 효율적이지만 탐지되기 쉽습니다.
- ISP프록시: 두 가지의 장점을 결합하여, 속도와 높은 신뢰 수준을 함께 제공합니다.
아래 예시는 Bright Data의 プロキ시 중 하나로 실행되도록 설정되어 있습니다. username, zone name, password를 본인의 자격 증명으로 반드시 교체하시기 바랍니다.
pytest proxy_test.py --proxy=brd-customer-<YOUR-USERNAME>-zone-<YOUR-ZONE-NAME>:<YOUR-PASSWORD>@brd.superproxy.io:33335 -s실행하면 다음과 같은 출력이 나타납니다:
=================================================== test session starts ===================================================
platform linux -- Python 3.10.12, pytest-8.3.4, pluggy-1.5.0
rootdir: /home/nultinator/clients/bright-data/seleniumbase-proxies
plugins: html-4.0.2, metadata-3.1.1, anyio-4.0.0, seleniumbase-4.34.6, ordering-0.6, rerunfailures-15.0, xdist-3.6.1
collected 1 item
proxy_test.py ip: 144.202.4.246
hostname: 144-202-4-246.lum-int.io
city: Piscataway
region: New Jersey
country: US
loc: 40.4993,-74.3990
org: AS20473 The Constant Company, LLC
postal: 08854
timezone: America/New_York
readme: https://ipinfo.io/missingauth
.
==================================================== 1 passed in 3.25s ====================================================
Bright Data의 プロキ시를 사용하면 country 플래그와 두 글자 국가 코드를 사용하여 위치를 선택할 수 있습니다:
pytest proxy_test.py --proxy=brd-customer-<YOUR-USERNAME>-zone-<YOUR-ZONE-NAME>:<YOUR-PASSWORD>-country-es@brd.superproxy.io:33335 -s국가 코드로 es(스페인)를 사용하면 스페인에 있는 プロキ시를 통해 라우팅됩니다. 아래 출력에서 이를 확인할 수 있습니다.
=================================================== test session starts ===================================================
platform linux -- Python 3.10.12, pytest-8.3.4, pluggy-1.5.0
rootdir: /home/nultinator/clients/bright-data/seleniumbase-proxies
plugins: html-4.0.2, metadata-3.1.1, anyio-4.0.0, seleniumbase-4.34.6, ordering-0.6, rerunfailures-15.0, xdist-3.6.1
collected 1 item
proxy_test.py ip: 176.119.14.158
city: Paracuellos de Jarama
region: Madrid
country: ES
loc: 40.5035,-3.5278
org: AS203020 HostRoyale Technologies Pvt Ltd
postal: 28860
timezone: Europe/Madrid
readme: https://ipinfo.io/missingauth
.
==================================================== 1 passed in 3.98s ====================================================
자세한 내용은 geolocation documentation을 참조하시기 바랍니다.
아래 코드는 국가 코드 세트를 사용하지만, 이는 실제 プロキ시 IP로도 쉽게 대체할 수 있습니다. countries에는 국가 코드 리스트가 들어 있습니다. 다음으로 이를 순회하면서 4개의 국가 코드 모두로 プロキ시 테스트를 실행합니다.
us: United Stateses: Spainil: Israelgb: Great Britain
import subprocess
#list of country codes
countries = [
"us",
"es",
"il",
"gb",
]
#iterate through the countries and make a shell command for each one
for country in countries:
command = f"pytest proxy_test.py --proxy=brd-customer-<YOUR-USERNAME>-<YOUR-ZONE-NAME>-country-{country}:[email protected]:33335 -s"
#run the shell command
subprocess.run(command, shell=True)이를 일반적인 Python 파일로 실행할 수 있습니다:
python rotate_proxies.py출력은 다음과 유사해야 합니다:
(Linux uses --headless by default. To override, use --headed / --gui. For Xvfb mode instead, use --xvfb. Or you can hide this info by using --headless / --headless2 / --uc.)
=================================================== test session starts ===================================================
platform linux -- Python 3.10.12, pytest-8.3.4, pluggy-1.5.0
rootdir: /home/nultinator/clients/bright-data/seleniumbase-proxies
plugins: html-4.0.2, metadata-3.1.1, anyio-4.0.0, seleniumbase-4.34.6, ordering-0.6, rerunfailures-15.0, xdist-3.6.1
collected 1 item
proxy_test.py ip: 164.90.142.33
city: Clifton
region: New Jersey
country: US
loc: 40.8344,-74.1377
org: AS14061 DigitalOcean, LLC
postal: 07014
timezone: America/New_York
readme: https://ipinfo.io/missingauth
.
==================================================== 1 passed in 3.84s ====================================================
(Linux uses --headless by default. To override, use --headed / --gui. For Xvfb mode instead, use --xvfb. Or you can hide this info by using --headless / --headless2 / --uc.)
=================================================== test session starts ===================================================
platform linux -- Python 3.10.12, pytest-8.3.4, pluggy-1.5.0
rootdir: /home/nultinator/clients/bright-data/seleniumbase-proxies
plugins: html-4.0.2, metadata-3.1.1, anyio-4.0.0, seleniumbase-4.34.6, ordering-0.6, rerunfailures-15.0, xdist-3.6.1
collected 1 item
proxy_test.py ip: 5.180.9.15
city: Madrid
region: Madrid
country: ES
loc: 40.4066,-3.6724
org: AS203020 HostRoyale Technologies Pvt Ltd
postal: 28007
timezone: Europe/Madrid
readme: https://ipinfo.io/missingauth
.
==================================================== 1 passed in 3.60s ====================================================
(Linux uses --headless by default. To override, use --headed / --gui. For Xvfb mode instead, use --xvfb. Or you can hide this info by using --headless / --headless2 / --uc.)
=================================================== test session starts ===================================================
platform linux -- Python 3.10.12, pytest-8.3.4, pluggy-1.5.0
rootdir: /home/nultinator/clients/bright-data/seleniumbase-proxies
plugins: html-4.0.2, metadata-3.1.1, anyio-4.0.0, seleniumbase-4.34.6, ordering-0.6, rerunfailures-15.0, xdist-3.6.1
collected 1 item
proxy_test.py ip: 64.79.233.151
city: Tel Aviv
region: Tel Aviv
country: IL
loc: 32.0809,34.7806
org: AS9009 M247 Europe SRL
timezone: Asia/Jerusalem
readme: https://ipinfo.io/missingauth
.
==================================================== 1 passed in 3.36s ====================================================
(Linux uses --headless by default. To override, use --headed / --gui. For Xvfb mode instead, use --xvfb. Or you can hide this info by using --headless / --headless2 / --uc.)
=================================================== test session starts ===================================================
platform linux -- Python 3.10.12, pytest-8.3.4, pluggy-1.5.0
rootdir: /home/nultinator/clients/bright-data/seleniumbase-proxies
plugins: html-4.0.2, metadata-3.1.1, anyio-4.0.0, seleniumbase-4.34.6, ordering-0.6, rerunfailures-15.0, xdist-3.6.1
collected 1 item
proxy_test.py ip: 185.37.3.107
city: London
region: England
country: GB
loc: 51.5085,-0.1257
org: AS9009 M247 Europe SRL
postal: E1W
timezone: Europe/London
readme: https://ipinfo.io/missingauth
.
==================================================== 1 passed in 2.90s ====================================================
SeleniumBase는 Selenium에서 많은 Web스크레이핑 기능을 가능하게 합니다. Bright Data의 업계 선도 プロキ시 서비스로 Selenium 기반 스크레이핑의 잠재력을 최대한 활용해 보시기 바랍니다. 지금 무료 체험을 시작해 보십시오!
