复制 def calculate_weight(proxy): # 响应时间越短,欧博abg权重越高 speed_weight = 1 / (proxy["avg_response_time"] + 0.1) # 成功率越高,欧博官网权重越高 success_weight = proxy["success_rate"] ** 2 return speed_weight * success_weight def get_weighted_proxy(): weighted_proxies = [(p, calculate_weight(p)) for p in all_proxies] total_weight = sum(w for _, w in weighted_proxies) rand = random.uniform(0, total_weight) current_sum = 0 for proxy, weight in weighted_proxies: current_sum += weight if current_sum > rand: return proxy return None (责任编辑:) |