-
安装 Selenium
本指南用到的 BiDi 网络处理程序是在 Selenium 4.32 中加入的。Selenium Manager 随包一同提供,并会在首次运行时获取 geckodriver,因此您只需安装 Firefox 即可。
Shell python -m pip install -U selenium -
将 Firefox 指向网关并完成登录
将 HTTP 和 SSL 代理首选项设为网关地址,启用 BiDi,并注册一个认证处理程序。网关发出 407 认证质询时,Selenium 会用您的用户名和密码进行应答。
Python from selenium import webdriver from selenium.webdriver.common.by import By options = webdriver.FirefoxOptions() options.enable_bidi = True options.set_preference("network.proxy.type", 1) options.set_preference("network.proxy.http", "gw.proxonym.com") options.set_preference("network.proxy.http_port", 8000) options.set_preference("network.proxy.ssl", "gw.proxonym.com") options.set_preference("network.proxy.ssl_port", 8000) driver = webdriver.Firefox(options=options) driver.network.add_auth_handler("USERNAME", "PASSWORD") driver.get("https://api.ipify.org") print("Exit IP:", driver.find_element(By.TAG_NAME, "body").text) driver.quit() -
每个浏览器保持一个 IP
Firefox 会在浏览器运行期间一直记住代理登录信息,因此请把一个 driver 视为一个身份。在用户名中加入
-session-ID,让所有连接在最长 120 分钟内都使用同一个 IP;需要新 IP 时,请启动一个新的 driver。Python import secrets from selenium import webdriver def new_driver(username, language=None): options = webdriver.FirefoxOptions() options.enable_bidi = True options.set_preference("network.proxy.type", 1) for scheme in ("http", "ssl"): options.set_preference(f"network.proxy.{scheme}", "gw.proxonym.com") options.set_preference(f"network.proxy.{scheme}_port", 8000) if language: options.set_preference("intl.accept_languages", language) driver = webdriver.Firefox(options=options) driver.network.add_auth_handler(username, "PASSWORD") driver.set_page_load_timeout(60) return driver session = secrets.token_hex(4) driver = new_driver(f"USERNAME-country-us-session-{session}-lifetime-60") -
定位地区并匹配语言
在用户名中添加国家、州或城市,并相应设置浏览器语言。借助上一步的
new_driver()辅助函数,两项设置都只需一行代码。Python from selenium.webdriver.common.by import By driver = new_driver("USERNAME-country-de-city-berlin-session-b3x9-lifetime-30", language="de-DE, de") driver.get("https://ipinfo.io/city") print("City:", driver.find_element(By.TAG_NAME, "body").text) driver.quit() -
从网关错误中恢复
当网关拒绝建立隧道时,
driver.get()会抛出一个提及about:neterror的 WebDriverException。请退出该 driver,并使用新的会话 ID 重试;如果反复出现proxyConnectFailure错误,则说明凭据有误。Python import secrets from selenium.common.exceptions import WebDriverException def open_with_retry(url, country="us", tries=3): for attempt in range(1, tries + 1): session = secrets.token_hex(4) driver = new_driver(f"USERNAME-country-{country}-session-{session}-lifetime-30") try: driver.get(url) return driver except WebDriverException as exc: driver.quit() print(f"attempt {attempt}:", str(exc).splitlines()[0]) if attempt == tries: raise driver = open_with_retry("https://example.com") print(driver.title) driver.quit()
常见问题
为什么不把凭据直接写在代理地址中?
浏览器会忽略代理设置中的用户名和密码,而 Selenium 的 proxy capability 也没有用于填写 HTTP 代理凭据的字段。因此,网关会对第一个请求返回 407 认证质询,再由 BiDi 认证处理程序用您的用户名和密码应答,就像真人在登录弹窗中输入一样。
可以用 Chrome 代替 Firefox 吗?
目前无法用这种方法实现。Chrome 会忽略其代理启动参数中的凭据,而且它的 BiDi 实现不会把代理登录质询传给 Selenium,因此处理程序永远不会被触发。对于 Chromium,请改用 Puppeteer 或 Playwright 驱动浏览器,它们原生支持代理登录;也可以使用配合 IP 白名单的共享数据中心代理,这样无需登录。
如何并行使用多个 IP?
每个身份启动一个 driver,各自在用户名中使用不同的会话 ID 或国家,并在不同的线程、进程或 Selenium Grid 节点中运行。Proxonym 不限制并发会话数,因此限制因素只有您的机器资源和目标网站所能承受的访问频率。
无头模式的 Firefox 能使用代理吗?
可以。在 Firefox 选项中添加 -headless 参数,代理首选项和认证处理程序保持不变即可。无头模式不会影响认证或定位,但会改变浏览器指纹的部分特征,因此请让语言和请求节奏与您选择的出口位置保持一致。
