
Selenium设置IP代理无效?先检查这几个常见坑点
很多人在用Selenium做自动化测试时,明明按照教程设置了代理IP,却发现完全没生效。页面还是显示自己的真实IP,或者直接报错打不开。这种情况多半是下面几个原因造成的:
代理IP格式写错了:这是最容易出错的地方。比如该用”http://”开头却写成了”https://”,或者端口号写成了字符串而不是数字。
代理IP本身已经失效:你用的代理IP可能已经过期或者被目标网站封禁了。特别是免费代理,存活时间往往很短。
浏览器启动时代理设置被覆盖:有些浏览器插件或者系统设置会覆盖你在代码中配置的代理。
代理协议不匹配:比如你的代理服务器只支持SOCKS5,但你却用HTTP协议去连接。
5种实用的解决方案
方案1:正确配置Chrome代理
这是最常用的方法,通过ChromeOptions来设置代理。注意格式一定要正确:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
proxy = "123.123.123.123:8080" 替换成你的代理IP和端口
chrome_options.add_argument(f'--proxy-server=http://{proxy}')
driver = webdriver.Chrome(options=chrome_options)
driver.get("http://httpbin.org/ip") 测试IP的网站
如果代理需要认证(用户名密码),就不能直接用上面的方法,需要安装插件来处理。
方案2:处理需要认证的代理
很多代理服务商如ipipgo提供的代理都需要认证,这时候需要用到下面的方法:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.proxy import Proxy, ProxyType
import zipfile
代理IP信息
proxy_host = "proxy.ipipgo.com" ipipgo代理服务器
proxy_port = "30001"
username = "your_username"
password = "your_password"
创建代理认证插件
def create_proxy_auth_extension(proxy_host, proxy_port, username, password):
manifest_json = """
{
"version": "1.0.0",
"manifest_version": 2,
"name": "Chrome Proxy",
"permissions": [
"proxy",
"tabs",
"unlimitedStorage",
"storage",
"",
"webRequest",
"webRequestBlocking"
],
"background": {
"scripts": ["background.js"]
}
}
"""
background_js = """
var config = {
mode: "fixed_servers",
rules: {
singleProxy: {
scheme: "http",
host: "%s",
port: parseInt(%s)
}
}
};
chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});
chrome.webRequest.onAuthRequired.addListener(
function(details) {
return {
authCredentials: {
username: "%s",
password: "%s"
}
};
},
{urls: [""]},
['blocking']
);
""" % (proxy_host, proxy_port, username, password)
pluginfile = 'proxy_auth_plugin.zip'
with zipfile.ZipFile(pluginfile, 'w') as zp:
zp.writestr("manifest.json", manifest_json)
zp.writestr("background.js", background_js)
return pluginfile
proxy_auth_plugin = create_proxy_auth_extension(proxy_host, proxy_port, username, password)
chrome_options = Options()
chrome_options.add_extension(proxy_auth_plugin)
driver = webdriver.Chrome(options=chrome_options)
driver.get("http://httpbin.org/ip")
方案3:使用DesiredCapabilities设置代理
这种方法更适合远程WebDriver或者Grid环境:
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
capabilities = DesiredCapabilities.CHROME.copy()
proxies = {
'proxyType': 'MANUAL',
'httpProxy': '123.123.123.123:8080',
'sslProxy': '123.123.123.123:8080'
}
capabilities['proxy'] = proxies
driver = webdriver.Chrome(desired_capabilities=capabilities)
方案4:定期更换代理IP
如果你在做大规模数据采集,同一个IP用太久容易被封。建议定时更换代理:
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
代理IP池
proxy_list = [
"111.111.111.111:8080",
"222.222.222.222:8080",
"333.333.333.333:8080"
]
def create_driver_with_proxy(proxy):
chrome_options = Options()
chrome_options.add_argument(f'--proxy-server=http://{proxy}')
return webdriver.Chrome(options=chrome_options)
每10分钟更换一次代理
for i, proxy in enumerate(proxy_list):
print(f"使用代理: {proxy}")
driver = create_driver_with_proxy(proxy)
执行你的自动化任务
driver.get("你的目标网站")
10分钟后关闭并更换代理
time.sleep(600)
driver.quit()
if i < len(proxy_list) - 1:
print("准备切换到下一个代理...")
方案5:选择高质量的代理服务
很多时候问题不在于代码,而在于代理IP的质量。免费代理通常存在这些问题:
| Type of problem | manifestations | prescription |
|---|---|---|
| Connection timeout | IP根本连不上 | 选择响应快的服务商 |
| IP blocked | 能连接但访问被拒 | 使用高质量住宅IP |
| 速度太慢 | Page load timeout | 选择专线代理 |
推荐使用专业的代理服务商如ipipgo,他们提供动态住宅代理IP资源总量高达9000万+,覆盖全球220+国家和地区,所有IP均来自真实家庭网络,具备高度匿名性。特别是他们的静态住宅代理,适合需要长期稳定IP的场景。
Why recommend ipipgo proxy service
在解决Selenium代理设置问题时,我测试过很多代理服务商,ipipgo在以下几个方面表现突出:
IP质量高:他们的动态住宅IP来自真实家庭网络,不容易被网站识别为代理,大大降低了被封的风险。
Wide coverage:9000万+IP资源覆盖220+国家地区,无论你需要哪个地区的IP都能满足。
Comprehensive protocol support:支持HTTP(S)和SOCKS5协议,兼容各种应用场景。
稳定性好:特别是静态住宅代理,99.9%的可用性保证了自动化任务的连续运行。
对于需要做大规模数据采集或者自动化测试的用户,建议选择ipipgo的动态住宅(标准)套餐,如果业务对IP稳定性要求极高,可以考虑他们的静态住宅代理。
Frequently Asked Questions QA
Q:设置了代理后Selenium启动特别慢怎么办?
A:这通常是代理IP速度慢导致的。建议更换响应更快的代理,或者使用ipipgo的静态住宅代理,他们的专线网络延迟很低。
Q:代理设置成功了,但某些网站还是打不开?
A:可能是目标网站封禁了代理IP。尝试更换其他IP,或者使用ipipgo的住宅IP,因为住宅IP更接近真实用户。
Q:如何验证代理是否真的生效了?
A:访问http://httpbin.org/ip或ip.ipipgo.com这样的IP检测网站,查看显示的IP是否已经变成你的代理IP。
Q:同一个代理IP可以多个Selenium实例同时使用吗?
A:这取决于代理服务商的政策。ipipgo的代理通常支持并发连接,但具体数量要看套餐限制。
Q:代理设置正确,但出现证书错误怎么办?
A:可以尝试添加--ignore-certificate-errors参数,或者使用SOCKS5协议可能避免这类问题。

