快速解答
如何在代码中使用静态 ISP 代理?
将代理 URL http://USERNAME:PASSWORD@IP:PORT(SOCKS5 则为 socks5h://)传入您的 HTTP 客户端:curl 的 --proxy、Python requests 的 proxies 参数,或 Node.js 中的 agent。PinnedIP 提供支持两种协议的专用美国 ISP IP 租用,US$2.03 起租 30 天(2026 年 10 月);无需 KYC,支持加密货币。
curl
curl 原生支持两种协议。socks5h 会让代理解析主机名。
curl -s --proxy http://USERNAME:[email protected]:HTTP_PORT https://api.ipify.org?format=json
curl -s --proxy socks5h://USERNAME:[email protected]:SOCKS_PORT https://api.ipify.org?format=jsonPython(requests)
安装 requests(SOCKS5 还需安装 requests[socks])。一个会话会复用连接,对目标网站友好且速度快。
import requests
PROXY = "http://USERNAME:[email protected]:8080" # or socks5h://...:SOCKS_PORT
session = requests.Session()
session.proxies = {"http": PROXY, "https": PROXY}
session.headers["User-Agent"] = "price-check/1.0 (contact: [email protected])"
response = session.get("https://api.ipify.org?format=json", timeout=20)
response.raise_for_status()
print(response.json()["ip"])
import itertools
import time
import requests
IPS = ["203.0.113.10", "203.0.113.11", "203.0.113.12"]
USER, PASSWORD, PORT = "USERNAME", "PASSWORD", 8080
URLS = ["https://example.com/", "https://example.org/"]
pool = itertools.cycle(IPS)
for url in URLS:
ip = next(pool)
proxy = f"http://{USER}:{PASSWORD}@{ip}:{PORT}"
r = requests.get(url, proxies={"http": proxy, "https": proxy}, timeout=20)
print(ip, url, r.status_code)
time.sleep(5) # keep a polite pace
Node.js
Node 内置的 fetch 不支持代理选项;对于 HTTP 代理,请使用 undici 包中的 ProxyAgent(Node 18 或更高版本)。
const { ProxyAgent, fetch } = require('undici');
const agent = new ProxyAgent('http://USERNAME:[email protected]:8080');
(async () => {
const res = await fetch('https://api.ipify.org?format=json', { dispatcher: agent });
const body = await res.json();
console.log(body.ip);
})();
自动化请求适用哪些规则?
以网站能够承受的节奏发送请求,尽可能标明您的客户端身份,遵守 robots.txt 和网站条款,并且不要收集您无权处理的个人数据。网上银行、政府和支付门户在所有 IP 上均被屏蔽,SMTP 端口 25、465 和 587 也同样被屏蔽。完整列表见可接受使用政策。
代码示例:常见问题
为什么使用 socks5h 而不是 socks5?
使用 socks5h 时代理会解析主机名,因此 DNS 查询也会通过美国 IP 进行。使用 socks5 时,则由您自己的机器先解析域名。
为什么我的请求返回 407?
代理未收到有效的凭据。请检查用户名和密码,或在客户区中将您自己的 IP 加入白名单并去掉凭据。
我可以在一个 IP 上并行运行大量请求吗?
可以,但需在合理使用范围内。许多网站会限制每个 IP 的请求数,因此请将高负载任务分散到多个 IP,并保持礼貌的速率。