python 检测免费代理ip是否有效

python 检测免费代理ip是否有效,免费ip获取地址https://www.zdaye.com/free/

import requests

IPAgents = [
    "218.89.51.167:9091",
    "120.37.121.209:9091",
    "113.195.207.249:9091",
    "113.195.207.249:9091",
    "111.47.170.136:9091",
    "111.72.218.180:9091",
    ]

#检查代理IP
def check_proxy(IPAgents):
    try:
        # 设置重连次数
        requests.adapters.DEFAULT_RETRIES = 3
        proxy = f"http://{IPAgents}"
        thisIP = "".join(IPAgents.split(":")[0:1])#现在ip去掉端口
        res = requests.get(url="http://icanhazip.com/",timeout=2,proxies={"http":proxy})
        proxyIP = res.text.strip()#返回的有效ip
        if(proxyIP == thisIP):
            print("代理IP:'"+ proxyIP + "'有效!")
        else:
            print("代理IP无效!")
    except Exception as e:
        print(str(e))

# 检查IP是否有效
for ckip in IPAgents:
    check_proxy(ckip)

python 检测免费代理ip是否有效_第1张图片

你可能感兴趣的:(python,开发语言)