国外IP代理推荐:
IPIPGO|全球住宅代理IP(>>>点击注册免费测试<<<)
国内IP代理推荐:
天启|全国240+城市代理IP(>>>点击注册免费测试<<<)
requests库代理设置详解
requests是Python中最常用的HTTP库,其代理配置非常简单。对于需要频繁更换IP的场景,使用ipipgo的代理服务能有效避免被目标网站封禁。

基本HTTP/HTTPS代理设置
只需在请求中传递proxies参数即可:
import requests
proxies = {
'http': 'http://用户名:密码@代理服务器:端口',
'https': 'https://用户名:密码@代理服务器:端口'
}
response = requests.get('http://httpbin.org/ip', proxies=proxies)
使用ipipgo代理的完整示例
ipipgo提供全协议支持,以下是使用住宅IP的示例:
import requests
从ipipgo获取的代理信息
ipipgo_proxy = {
'http': 'http://your-ipipgo-username:password@proxy.ipipgo.com:8080',
'https': 'https://your-ipipgo-username:password@proxy.ipipgo.com:8080'
}
try:
response = requests.get('目标网址', proxies=ipipgo_proxy, timeout=10)
print(f"请求成功,使用的IP地址:{response.json()['origin']}")
except Exception as e:
print(f"请求失败:{e}")
会话级别的代理设置
如果需要保持会话,可以使用Session对象:
session = requests.Session()
session.proxies.update(ipipgo_proxy)
后续所有请求都会自动使用代理
response1 = session.get('网址1')
response2 = session.get('网址2')
urllib3库代理配置技巧
urllib3是requests库的底层依赖,直接使用urllib3时,代理设置略有不同。
基本代理配置
import urllib3
http = urllib3.ProxyManager(
'http://用户名:密码@代理服务器:端口/',
headers={'User-Agent': 'Mozilla/5.0'}
)
response = http.request('GET', '目标网址')
高级配置示例
urllib3支持更细粒度的控制:
import urllib3
使用ipipgo代理并设置连接池
ipipgo_proxy_url = 'http://your-username:password@proxy.ipipgo.com:8080'
proxy_manager = urllib3.ProxyManager(
ipipgo_proxy_url,
num_pools=5, 连接池数量
maxsize=10, 每个池最大连接数
timeout=urllib3.Timeout(connect=5.0, read=10.0)
)
response = proxy_manager.request('GET', '目标网址')
Selenium浏览器代理配置大全
Selenium通过浏览器驱动进行网络请求,代理设置需要针对不同浏览器进行配置。
Chrome浏览器代理设置
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument('--proxy-server=http://代理服务器:端口')
driver = webdriver.Chrome(options=chrome_options)
driver.get("目标网址")
带认证的代理设置
对于需要用户名密码认证的ipipgo代理:
from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.common.proxy import Proxy, ProxyType proxy = Proxy() proxy.proxy_type = ProxyType.MANUAL proxy.http_proxy = "代理服务器:端口" proxy.ssl_proxy = "代理服务器:端口" 处理认证(需要配合插件或扩展) capabilities = webdriver.DesiredCapabilities.CHROME proxy.add_to_capabilities(capabilities) driver = webdriver.Chrome(desired_capabilities=capabilities)
Firefox浏览器代理设置
from selenium import webdriver
profile = webdriver.FirefoxProfile()
profile.set_preference("network.proxy.type", 1)
profile.set_preference("network.proxy.http", "代理服务器")
profile.set_preference("network.proxy.http_port", 端口)
profile.set_preference("network.proxy.ssl", "代理服务器")
profile.set_preference("network.proxy.ssl_port", 端口)
driver = webdriver.Firefox(firefox_profile=profile)
不同场景下的代理选择策略
根据业务需求选择代理类型
| 场景 | 推荐代理类型 | ipipgo方案优势 |
|---|---|---|
| 数据采集 | 住宅IP | 真实家庭IP,不易被识别 |
| 测试验证 | 静态住宅IP | IP稳定,适合长时间任务 |
| 高频访问 | 动态住宅IP | 自动切换ip,避免封禁 |
代理IP轮换策略
对于需要大量IP轮换的场景,可以结合ipipgo的API实现自动切换:
import requests
import time
def get_ipipgo_proxy_list():
调用ipipgo API获取代理列表
返回格式:['ip:port', 'ip:port', ...]
pass
proxy_list = get_ipipgo_proxy_list()
current_proxy_index = 0
def get_next_proxy():
global current_proxy_index
proxy = proxy_list[current_proxy_index]
current_proxy_index = (current_proxy_index + 1) % len(proxy_list)
return {
'http': f'http://{proxy}',
'https': f'https://{proxy}'
}
使用轮换代理发送请求
for i in range(10):
proxies = get_next_proxy()
response = requests.get('目标网址', proxies=proxies)
time.sleep(1) 控制请求频率
常见问题与解决方案
Q1: 代理连接超时怎么办?
A: 检查代理服务器地址和端口是否正确,确认网络连接正常。ipipgo代理服务提供99.9%的可用性保证,如遇问题可尝试更换IP节点。
Q2: 如何验证代理是否生效?
A: 通过访问http://httpbin.org/ip等IP查询服务,对比返回的ip地址是否与预期一致。
Q3: 代理认证失败如何解决?
A: 确认用户名密码正确,检查认证格式是否符合要求。ipipgo代理支持多种认证方式,可根据文档选择适合的方案。
Q4: Selenium代理设置不生效怎么办?
A: 检查浏览器启动参数是否正确,确认代理协议与浏览器兼容。对于复杂场景,建议使用ipipgo提供的专用浏览器扩展。
通过合理配置代理ip,结合ipipgo高质量的住宅IP资源,可以有效提升网络请求的成功率和稳定性。在实际使用中,建议根据具体需求选择合适的代理方案,并做好错误处理和日志记录。
国外IP代理推荐:
IPIPGO|全球住宅代理IP(>>>点击注册免费测试<<<)
国内ip代理推荐:
天启|全国240+城市代理IP(>>>点击注册免费测试<<<)
















发表评论
发表评论: