Free Proxy Health List

实时快照 · 3,287 proxies

搭建轮换代理池

约 20 行代码实现带健康状态跟踪的可用轮换逻辑。

搭建轮换代理池

1. 带失败计数的轮换

import collections, itertools, requests

proxies = [l.strip() for l in open("proxies.txt") if l.strip()]
failures = collections.Counter()
pool = itertools.cycle(proxies)

def get(url, attempts=10):
    for _ in range(attempts):
        proxy = next(pool)
        if failures[proxy] >= 3:
            continue
        try:
            return requests.get(
                url,
                proxies={"http": f"http://{proxy}", "https": f"http://{proxy}"},
                timeout=8,
            )
        except requests.RequestException:
            failures[proxy] += 1
    raise RuntimeError("pool exhausted")

能帮你省时间的说明

按代理统计失败次数并跳过屡次失败者,才是把原始列表变成可用代理池的关键 —— 否则你会为同样那些失效条目反复付出超时代价。请按发布频率刷新文件,并在刷新时重置计数。如果是无法承受这个失败率的生产负载,维护良好的轮换端点是更合适的工具。

相关教程

公开快照的使用边界
免费公共代理由不明身份的第三方运营且被多人共享,切勿通过它们传输密码、令牌或个人数据。请遵守 GitHub 可接受使用政策:不得用于垃圾信息、攻击、绕过访问控制,或违反网站政策的采集行为。