简要回答
在 Python requests 中传入 proxies 字典,http 和 https 使用同一个代理 URL,由用户名、密码、gate.hiproxy.shop 和 6100 端口组成。使用 SOCKS5 时,安装 requests[socks],并用 socks5h 和 6200 端口。不加 -sid- 时每个请求换新 IP,加上 -sid- 和 -ttl- 可保持同一 IP 最长 60 分钟。
Python requests 代理:基础示例
为 Python requests 设置代理只需要一个字典。在 http 和 https 两个键下放同一个代理 URL。https 键对应 https:// 地址,这些请求依然以隧道方式经过 HTTP 端口。
输出的地址就是请求使用的 IP。把 hpexample1 和 your_password 换成控制台中的代理用户名和密码。请始终设置超时,避免一个慢请求卡住整个脚本。
import requests
proxy = "http://hpexample1-zone-res-cc-us:your_password@gate.hiproxy.shop:6100"
proxies = {"http": proxy, "https": proxy}
r = requests.get("https://api.ipify.org", proxies=proxies, timeout=30)
print(r.text)在 Python 中使用 SOCKS5
requests 支持 SOCKS 需要额外安装一个包:pip install requests[socks]。之后把协议改为 socks5h,端口改为 6200,其他都不变。
socks5h 表示由代理解析域名。如果用普通的 socks5,您的电脑会先做 DNS 查询,这个查询会从您自己的网络发出。
# pip install requests[socks]
import requests
proxy = "socks5h://hpexample1-zone-res-cc-us:your_password@gate.hiproxy.shop:6200"
proxies = {"http": proxy, "https": proxy}
r = requests.get("https://api.ipify.org", proxies=proxies, timeout=30)
print(r.text)轮换和粘性会话
默认每个请求都获得新 IP,适合请求之间互不依赖的数据采集。要在多个请求中保持同一 IP,例如登录和之后的页面,加上带任意 ID 的 -sid-,再用 -ttl- 设置分钟数,最长 60。使用相同 ID 的请求都使用同一个 IP。
| 参数 | 示例 | 作用 |
|---|---|---|
| -zone- | -zone-res | 产品:res、unl、isp 或 dc |
| -cc- | -cc-us | 出口国家,两位小写字母 |
| -city- | -city-new_york | 城市,空格用下划线代替 |
| -sid- | -sid-a1b2c3d4 | 使用此 ID 的请求都用同一个 IP |
| -ttl- | -ttl-30 | 会话保持 IP 的分钟数,1~60 |
import uuid
import requests
sid = uuid.uuid4().hex[:8]
user = f"hpexample1-zone-res-cc-us-sid-{sid}-ttl-30"
proxy = f"http://{user}:your_password@gate.hiproxy.shop:6100"
s = requests.Session()
s.proxies = {"http": proxy, "https": proxy}
print(s.get("https://api.ipify.org", timeout=30).text)
print(s.get("https://api.ipify.org", timeout=30).text) # 同一个 IPNode.js 和 cURL 示例
在 Node.js 中,undici 包通过 ProxyAgent 为 fetch 设置代理。使用 SOCKS5 时,socks-proxy-agent 包用 6200 端口的 socks5h:// URL 完成同样的工作。
使用 cURL 时,用 -x 指定代理,用 -U 指定登录信息:curl -x http://gate.hiproxy.shop:6100 -U "USERNAME:PASSWORD" https://api.ipify.org。使用 SOCKS5 时改用 socks5h://gate.hiproxy.shop:6200。不加 -sid- 运行两次,应该会看到两个不同的 IP。
import { ProxyAgent, fetch } from "undici";
const agent = new ProxyAgent(
"http://hpexample1-zone-res-cc-us:your_password@gate.hiproxy.shop:6100"
);
const res = await fetch("https://api.ipify.org", { dispatcher: agent });
console.log(await res.text());解决 407 错误和超时
记录失败请求的状态码和出口 IP。这样更容易分辨是代理登录问题,还是目标网站的拦截。
| 错误 | 可能原因 | 解决方法 |
|---|---|---|
| 407 Proxy Authentication Required(代理认证失败) | 代理用户名或密码错误,或参数有拼写错误 | 从控制台重新复制两者。如果生成了新的代理密码,请更新脚本。 |
| Connection timed out(连接超时) | 端口与协议不匹配,或防火墙拦截了出站连接 | http:// 用 6100,socks5h:// 用 6200。允许到 gate.hiproxy.shop 的出站流量。 |
| Missing dependencies for SOCKS support(缺少 SOCKS 依赖) | 没有安装 requests[socks] | 运行 pip install requests[socks]。 |
| IP 国家不对 | -cc- 不是两位小写字母代码 | 代码用小写,例如 -cc-us。 |
| 部分请求出现 Read timed out(读取超时) | 目标网站或线路较慢 | 捕获异常后重试。不加 -sid- 时,重试会使用新 IP。 |
在服务器上用 IP 白名单运行脚本
如果脚本运行在有固定 IPv4 地址的服务器上,可以在控制台把该地址加入 IP 白名单。最多可添加 10 个地址。来自这些服务器的请求无需代理密码即可连接。
无论哪种方式,都不要把代理密码写进要分享或提交的代码里。请改为从环境变量读取,一旦泄露,就在控制台生成新密码。