
IPRoyal 住宅 IP 池有 2000 万+ IP,关键是怎么用。粘性(Sticky)一个 IP 用 30 分钟适合账号登录场景,轮换(Rotating)每次请求换 IP 适合爬虫、批量注册、反风控。本文用 Python 实现轮换 + 重试的完整方案。
两种模式区别
| 模式 | IP 切换频率 | 适用场景 |
|---|---|---|
| Sticky | 每 10/30 分钟换一次 | 账号登录、API 调用、量化持续会话 |
| Rotating | 每次请求随机换 | 爬虫、批量注册、反风控 |
IPRoyal 后台可以生成两种模式的代理凭证。
Rotating 代理凭证格式
登录 IPRoyal Dashboard → Residential → Generate → 选 Rotating → 拿到:
Proxy: gw.iproyal.com
Port: 12321
Username: YOUR_USER
Password: YOUR_PASS_country-us_session-rotate
session-rotate 是关键:每次发请求都会从池子里抓新 IP。
Python 基础用法
import requests
proxy_url = "http://USER:[email protected]:12321"
proxies = {"http": proxy_url, "https": proxy_url}
for i in range(5):
r = requests.get("https://ipinfo.io/json", proxies=proxies, timeout=10)
print(f"Request {i+1}: {r.json()['ip']} ({r.json().get('city')})")
预期输出:
Request 1: 142.176.xx.xx (San Diego)
Request 2: 71.192.xx.xx (New York)
Request 3: 98.44.xx.xx (Austin)
Request 4: 24.128.xx.xx (Boston)
Request 5: 104.128.xx.xx (Chicago)
每次请求都是不同的住宅 IP,像不同地方的普通家庭用户在访问。
实战 1:爬虫反封禁
爬大型网站(如亚马逊商品价、Google 搜索结果)时经常被 ban。轮换 IP 让目标网站认为你是"很多不同用户"而不是机器人。
import requests
import time
from urllib.parse import quote
proxy = "http://USER:[email protected]:12321"
PROXIES = {"http": proxy, "https": proxy}
def scrape_url(url, max_retries=3):
"""每次失败自动换 IP 重试"""
for attempt in range(max_retries):
try:
# session-rotate 保证每次重试都是新 IP
r = requests.get(url, proxies=PROXIES, timeout=15,
headers={"User-Agent": "Mozilla/5.0 ..."})
if r.status_code == 200:
return r.text
elif r.status_code in (403, 429, 503):
print(f" 被 ban ({r.status_code}),换 IP 重试 [{attempt+1}]")
time.sleep(2)
continue
else:
print(f" HTTP {r.status_code}")
return None
except requests.Timeout:
print(f" 超时,重试 [{attempt+1}]")
time.sleep(3)
except requests.RequestException as e:
print(f" error: {e}")
time.sleep(2)
return None
# 示例:爬 100 个 URL
urls = [f"https://example.com/product/{i}" for i in range(100)]
for u in urls:
html = scrape_url(u)
if html: process(html) # 你的解析逻辑
time.sleep(0.5) # 温和一点
关键:session-rotate 让每次 requests.get() 都用新 IP,所以被 ban 的请求重试时自动换 IP,不需要手动切换。
实战 2:绕过地区限制
某些网站只对特定地区开放。IPRoyal 支持指定国家:
# 美国 IP
us_proxy = "http://USER:[email protected]:12321"
# 日本 IP
jp_proxy = "http://USER:[email protected]:12321"
# 英国 IP
uk_proxy = "http://USER:[email protected]:12321"
def get_by_country(url, country="us"):
proxy_url = f"http://USER:PASS_country-{country}[email protected]:12321"
return requests.get(url, proxies={"http": proxy_url, "https": proxy_url}).text
# 爬 Netflix 美国和日本的不同内容
html_us = get_by_country("https://netflix.com/browse", "us")
html_jp = get_by_country("https://netflix.com/browse", "jp")
实战 3:批量账号注册
(法律注意:请确保目标网站 ToS 允许。常见合规场景:同人邮箱注册多个正当业务账号,或测试用途)
import random
def register_account(email, password, country="us"):
proxy_url = f"http://USER:PASS_country-{country}[email protected]:12321"
PROXIES = {"http": proxy_url, "https": proxy_url}
# 模拟真人行为:随机延迟
time.sleep(random.uniform(2, 5))
# 访问注册页先(获取 CSRF token)
session = requests.Session()
session.proxies = PROXIES
session.headers["User-Agent"] = random_user_agent()
resp = session.get("https://target.com/register")
csrf = extract_csrf(resp.text)
time.sleep(random.uniform(3, 8)) # 模拟"填写表单"时间
resp = session.post("https://target.com/register", data={
"email": email,
"password": password,
"_csrf": csrf,
})
return resp.status_code == 200
# 批量
emails = load_emails()
for email in emails:
success = register_account(email, "StrongPass123!", random.choice(["us", "gb", "ca"]))
print(f"{email}: {'✓' if success else '✗'}")
time.sleep(random.uniform(30, 120)) # 账号间足够间隔
实战 4:量化策略的请求分散
Binance 对单个 IP 有 rate limit(每分钟请求数)。如果量化策略频繁调用,轮换 IP 能绕开这个限制:
import random
PROXY_POOL = [
f"http://USER:[email protected]:12321",
f"http://USER:[email protected]:12321",
f"http://USER:[email protected]:12321",
]
def binance_request(path):
"""分散请求到不同地区的住宅 IP"""
proxy = random.choice(PROXY_POOL)
return requests.get(f"https://api.binance.com{path}",
proxies={"https": proxy}, timeout=5)
# 并发用这个函数,每次请求都是新地区的新 IP
失败重试的更好姿势(用 tenacity)
手写重试容易漏情况。用 tenacity 库:
from tenacity import retry, stop_after_attempt, wait_exponential
@retry(stop=stop_after_attempt(5), wait=wait_exponential(multiplier=1, min=2, max=30))
def scrape_with_retry(url):
r = requests.get(url, proxies=PROXIES, timeout=15)
r.raise_for_status() # 4xx/5xx 抛异常触发重试
return r.text
自动指数回退(2s, 4s, 8s, 16s, 30s),且 session-rotate 保证每次都是新 IP。
费用对比
| 方案 | 月成本(10GB 流量) | 稳定性 | IP 质量 |
|---|---|---|---|
| IPRoyal Residential Rotating | $70 | ⭐⭐⭐⭐⭐ | 家庭宽带 |
| Bright Data | $300 | ⭐⭐⭐⭐⭐ | 同上但更贵 |
| 免费代理列表 | $0 | ⭐ | 95% 不可用 |
| VPS 自建 | $20-50 | ⭐⭐⭐ | 机房 IP,易封 |
IPRoyal 是性价比最高的,但贵在流量消耗(按 GB 付费)。爬大图/视频的场景流量消耗快,要算清楚。
总结
IPRoyal Rotating 模式解决:
- 爬虫反封禁
- 地区限制绕过
- 批量操作
- API rate limit 分散
Python 配合 session-rotate 后缀实现自动换 IP。失败重试用 tenacity 更优雅。
想开始: → IPRoyal 按流量付费,预充 $5 就能用(2000 万+ 住宅 IP 池)
注意事项:
- 遵守目标网站 ToS
- 爬虫加合理延迟,别把对方服务器 DDoS 了
- 数据保密(IPRoyal 看得到你的请求 URL 但看不到内容,HTTPS 加密)
延伸阅读: