# Selenium Python (4). PythonとプロキシでSeleniumを使う方法

PythonでSeleniumのパワーを体験し、プロキシを活用して効率的なWebスクレイピングと自動化を実現しましょう。FineProxyの手頃な価格のプロキシサーバーでデータ収集を強化しましょう。

- 著者: [Brandon Perry](https://fineproxy.org/ja/brandon-perry/)
- 公開日: 2024-06-04T11:17:19.000Z
- 更新日: 2024-05-27T11:43:12.000Z
- 正規URL: https://fineproxy.org/ja/selenium-python-4-how-to-use-selenium-with-python-and-proxies/

Selenium と Python を使用してウェブスクレイピングまたは自動化を行う場合、プロキシを使用することは IP バンを回避してデータ収集効率を向上させるために不可欠です。この記事では、Selenium で Python を使用してプロキシを設定および使用する方法について説明します。

## Selenium とは何か、そしてなぜプロキシを使用するのか

**Selenium** はウェブブラウザの自動化に強力なツールであり、ウェブスクレイピングやウェブアプリケーションのテストによく使用されます。 **プロキシ** エンドユーザーと彼らがアクセスするウェブサイトの間に位置する中間サーバーで、IP アドレスをマスクしてリクエスト負荷を管理するのに役立ちます。

## プロキシを使用したSeleniumのセットアップ

コードに進む前に、必要なツールがインストールされていることを確認してください:

-   Python
-   Selenium (`pip install selenium`)
-   ウェブブラウザドライバ（例：ChromeのChromedriverまたはFirefoxのGeckoDriver）

## Selenium でプロキシを使用するためのステップバイステップガイド

### 1\. プロキシを使用して Chrome を設定する

Chrome をプロキシサーバーを使用するよう設定するには、Selenium で必要な機能を設定する必要があります。

```python
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` class from `selenium.webdriver.common.proxy`.

```python
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 プロファイル設定を変更します。

```python
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 | FirefoxProfile | あり | あり |

これらのステップに従うことで、スクレイピング活動が中断されず効率的に実行されることを保証できます。ご質問や今後のチュートリアルのアイデアがあれば、お気軽にコメントをお寄せください！
