Selenium 轮换代理:在 Selenium WebDriver 中轮换 IP(新手指南)
一份面向新手的 Selenium WebDriver 代理轮换指南。设置代理列表、处理身份验证、验证 IP 是否变化,并了解何时 NordLayer 的专用 WireGuard 网关才是更明智的选择。
概述
Selenium WebDriver 本身不会轮换代理。当你进行大规模抓取或测试时,单一 IP 地址很快就会触达速率限制、地域封锁或 CAPTCHA。本教程将演示如何为 Selenium 指定代理、在一组代理之间轮换、验证 IP 是否真的发生了变化,以及判断何时专用 WireGuard 网关(例如 NordLayer)比轮换代理池更合适。
为什么要在 Selenium 中轮换代理?
- 避免基于 IP 的速率限制和临时封禁。
- 访问仅在特定地区可用的内容。
- 测试你的网站或应用在不同地理位置的表现。
- 分散请求,避免单一 IP 看起来像机器人。
轮换策略
根据你需要发送的请求数量和每个会话需要保持的时长来选择策略。
- 基于列表的轮换 —— 在一份固定的代理列表中循环使用。简单且免费,但每个代理都会被重复使用。
- 随机轮换 —— 为每个会话随机挑选一个代理。减少可预测的模式。
- 轮询(Round-robin) —— 按顺序使用代理。适合均匀分配。
- 粘性会话 —— 在登录或结账流程中保持使用同一个代理,之后再切换。
- 轮换网关 —— 由服务商侧为你轮换 IP 的单一端点。代码更少,但控制力也更弱。
对于许多小型项目来说,从列表中随机挑选一个代理就足够了。对于生产环境的抓取,可以考虑使用托管式轮换代理服务,或使用专用 WireGuard 网关来获得稳定、不轮换的访问。
前置条件
- Python 3.8 或更高版本。
- 已安装 Google Chrome 或 Mozilla Firefox。
- 一份代理列表(HTTP、HTTPS 或 SOCKS5),或一个轮换代理订阅。
- 基本熟悉如何运行 Python 脚本。
操作步骤
-
安装 Selenium 和一个 WebDriver 管理器。
安装 Selenium 以及一个可自动下载正确浏览器驱动的辅助工具。
pip install selenium webdriver-manager -
准备你的代理列表。
在 Python 中创建一个代理列表。使用完整的协议前缀,以便 Selenium 知道如何连接。
PROXIES = [ 'http://user:[email protected]:8080', 'http://proxy2.example.com:8080', 'socks5://proxy3.example.com:1080', ]如果你的代理需要用户名和密码,请先查看第 4 步再继续。
-
在 Selenium 中配置单个代理(Chrome)。
使用
--proxy-server参数将代理传给 Chrome。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 = 'http://proxy2.example.com:8080' options = Options() options.add_argument(f'--proxy-server={proxy}') options.add_argument('--headless=new') driver = webdriver.Chrome( service=Service(ChromeDriverManager().install()), options=options ) driver.get('https://api.ipify.org?format=json') print(driver.page_source) driver.quit()Chrome 不接受在
--proxy-server字符串中包含凭据。如果你的代理需要身份验证,请使用 IP 白名单,或改用 Firefox(第 4 步)。 -
处理代理身份验证。
Firefox 配置文件可以发送代理凭据。这对于不需要额外库的新手友好型配置很有用。
from selenium import webdriver from selenium.webdriver.firefox.options import Options proxy_host = 'proxy2.example.com' proxy_port = 8080 options = Options() options.set_preference('network.proxy.type', 1) options.set_preference('network.proxy.http', proxy_host) options.set_preference('network.proxy.http_port', proxy_port) options.set_preference('network.proxy.ssl', proxy_host) options.set_preference('network.proxy.ssl_port', proxy_port) driver = webdriver.Firefox(options=options) driver.get('https://api.ipify.org?format=json') print(driver.page_source) driver.quit()对于需要身份验证的 SOCKS5,对新手来说最简单的做法是向你的代理服务商申请 IP 白名单访问。这样你就不必在脚本中嵌入密码。
-
按会话轮换代理。
为每个 URL 或任务创建一个全新的 WebDriver,并随机选择一个代理。完成后务必退出 driver。
import random from selenium import webdriver from selenium.webdriver.chrome.options import Options from webdriver_manager.chrome import ChromeDriverManager from selenium.webdriver.chrome.service import Service PROXIES = [ 'http://proxy1.example.com:8080', 'http://proxy2.example.com:8080', 'socks5://proxy3.example.com:1080', ] def get_driver(): proxy = random.choice(PROXIES) options = Options() options.add_argument(f'--proxy-server={proxy}') options.add_argument('--headless=new') return webdriver.Chrome( service=Service(ChromeDriverManager().install()), options=options ) urls = [ 'https://example.com/page-1', 'https://example.com/page-2', ] for url in urls: driver = get_driver() try: driver.get(url) print(driver.title) finally: driver.quit()这种写法易于阅读,但为每个请求都启动一个新浏览器速度很慢。对于规模更大的任务,可以并行运行多个 driver,或使用轮换网关端点。如果你需要为登录或管理后台使用稳定、不轮换的 IP,请直接跳到第 7 步。
-
验证 IP 地址已更改。
切勿想当然地认为代理已生效。在抓取之前先检查出口 IP。
import json from selenium import webdriver from selenium.webdriver.chrome.options import Options options = Options() options.add_argument('--proxy-server=http://proxy2.example.com:8080') options.add_argument('--headless=new') driver = webdriver.Chrome(options=options) driver.get('https://api.ipify.org?format=json') body = driver.find_element('tag name', 'body').text ip_info = json.loads(body) print(ip_info['ip']) driver.quit()如果打印出的 IP 与你真实的 IP 相同,说明代理并未被使用。请检查代理格式和身份验证配置。
-
在轮换代理与专用 WireGuard 网关之间做选择。
轮换代理非常适合匿名的、大批量请求。但有些任务需要的恰恰相反:一个稳定的、企业级的 IP,并且每个请求都保持不变。
当你需要以下情况时,请使用专用 WireGuard 网关,例如 NordLayer:
- 为团队成员或自动化任务提供一致的出口 IP。
- 使用加密隧道,而不是为每个应用单独配置代理。
- 让小团队快速上手,无需维护代理列表。
- 为内部工具、预发布站点或从单一地区进行排名追踪提供独立的网络路径。
NordLayer 提供企业级 WireGuard VPN 隧道,配备专用网关和统一的月度计费。它并不是轮换代理池,因此请将其用于必须保持同一 IP 的会话。当每个请求都应来自不同地址时,请使用轮换代理。
故障排查
| 症状 | 可能原因 | 解决方法 |
|---|---|---|
| 代理连接被拒绝 | 主机、端口或协议错误 | 确认代理字符串包含 http:// 或 socks5:// |
| 407 Proxy Authentication Required | 缺少凭据 | 使用 IP 白名单,或使用带身份验证的 Firefox 配置文件 |
| 无法创建 WebDriver 会话 | 浏览器/驱动版本不匹配 | 更新 Selenium 和 webdriver-manager |
| 请求仍然显示你的真实 IP | 代理未生效 | 确认在 driver.get() 之前已设置 --proxy-server |
| 出现 CAPTCHA 或拦截页面 | 请求过多或代理已被标记 | 放慢速度、更谨慎地轮换,或使用住宅/ISP IP |
| SOCKS5 代理在 Chrome 中失败 | Chrome 不支持在该参数中为 SOCKS5 设置代理认证 | 使用 Firefox,或使用 NordLayer 之类带身份验证的企业隧道 |
总结
- Selenium 不会自动轮换代理;你需要为每个浏览器会话指定一个代理。
- 对于新手,可以先从一份小型代理列表、随机选择以及每个任务使用全新 driver 开始。
- Firefox 配置文件比 Chrome 更容易处理代理身份验证。
- 在信任代理之前,务必用 ipify 之类的服务验证出口 IP。
- 轮换代理最适合分布式、匿名的抓取。
- 当你需要为团队和长时间会话提供稳定、加密、企业级的 IP 时,像 NordLayer 这样的专用 WireGuard 网关是更好的选择。