このガイドでは、制限を回避し、Webスクレイピングの成功率を高めるために、認証付きプロキシで SeleniumBase をセットアップする方法を解説します。
Selenium は認証付きプロキシをサポートしていません。SeleniumWire はすでに 1 年以上前に非推奨となっており、さらに 2 年以上更新がありません。そのため、解決策として SeleniumBase を使用します。後者のプロジェクトは元々、Selenium インスタンスを実行して Python で Web 自動化とテストを行うためのラッパーとして設計されました。しかし、私たちにとっては、認証付きプロキシを使用して 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 にユーザー名とパスワードを含めるだけです。
proxy=<YOUR_USERNAME>:<YOUR_PASSWORD>@<PROXY_URL>:<PROXY_PORT>
最適な認証付きプロキシの選択肢は次のとおりです。
- Residential proxies: 実ユーザーの IP を使用するため、ボット検知の回避に最適です。
- Datacenter proxies: より高速でコスト効率が良い一方、検知されやすいです。
- ISP proxies: 両方のメリットを組み合わせ、高い信頼性を保ちながら速度も提供します。
以下の例は、Bright Data のプロキシの 1 つで実行するように設定されています。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 フラグと 2 文字の国コードを使用してロケーションを選択できます。
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 ベースのスクレイピングの可能性を最大限に引き出してください。今すぐ無料トライアルを開始しましょう。
