Python 快速验证代理IP是否有效的方法实现

一颗松果
一颗松果
发布时间:2026-10-14| 5人浏览过
原创

本文介绍如何使用Python的requests库快速验证代理IP的有效性。通过模拟HTTP请求访问指定网站,根据响应结果判断代理是否可用。文章提供了完整的Python代码示例,包含IP列表、随机选择代理、异常处理及结果输出,帮助开发者在爬虫项目中高效筛选有效代理。

Python验证代理IP有效性的原理与方法

在爬虫开发等场景中,代理IP常被用于隐藏真实IP或突破访问限制。获取一批代理IP后,如何确认其是否可用是一个常见问题。本文介绍一种基于Python的验证方法:通过携带该IP发起HTTP请求,若多次尝试均无法成功访问目标网站,则判定该代理无效。

具体实现代码如下:

Python验证代理IP的完整代码示例


import requests
import random
import time

http_ip = [
    '118.163.13.200:8080',
    '222.223.182.66:8000',
    '51.158.186.242:8811',
    '171.37.79.129:9797',
    '139.255.123.194:4550'
]

for i in range(10):
    try:
        ip_proxy = random.choice(http_ip)
        proxy_ip = {
            'http': ip_proxy,
            'https': ip_proxy,
        }
        print('使用代理的IP:', proxy_ip)
        response = requests.get("http://httpbin.org/ip", proxies=proxy_ip).text
        print(response)
        print('当前IP有效')
        time.sleep(2)
    except Exception as e:
        print(e.args[0])
        print('当前IP无效')
        continue

程序运行后的输出结果如下所示:

使用代理的IP: {'http': '118.163.13.200:8080', 'https': '118.163.13.200:8080'} HTTPConnectionPool(host='118.163.13.200', port=8080): Max retries exceeded with url: http://httpbin.org/ip (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError('<urllib3.connection.HTTPConnection object at 0x00000247674F5F88>: Failed to establish a new connection: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。'))) 当前IP无效 使用代理的IP: {'http': '51.158.186.242:8811', 'https': '51.158.186.242:8811'} { "origin": "51.158.186.242" }

当前IP有效 使用代理的IP: {'http': '222.223.182.66:8000', 'https': '222.223.182.66:8000'} { "origin": "139.202.62.84, 222.223.182.66" }

当前IP有效 使用代理的IP: {'http': '51.158.186.242:8811', 'https': '51.158.186.242:8811'} { "origin": "51.158.186.242" }

当前IP有效 使用代理的IP: {'http': '51.158.186.242:8811', 'https': '51.158.186.242:8811'} { "origin": "51.158.186.242" }

当前IP有效 使用代理的IP: {'http': '222.223.182.66:8000', 'https': '222.223.182.66:8000'} HTTPConnectionPool(host='222.223.182.66', port=8000): Max retries exceeded with url: http://httpbin.org/ip (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError('<urllib3.connection.HTTPConnection object at 0x00000247675067C8>: Failed to establish a new connection: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。'))) 当前IP无效 使用代理的IP: {'http': '139.255.123.194:4550', 'https': '139.255.123.194:4550'} HTTPConnectionPool(host='139.255.123.194', port=4550): Max retries exceeded with url: http://httpbin.org/ip (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError('<urllib3.connection.HTTPConnection object at 0x00000247674F55C8>: Failed to establish a new connection: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。'))) 当前IP无效 使用代理的IP: {'http': '51.158.186.242:8811', 'https': '51.158.186.242:8811'} { "origin": "51.158.186.242" }

当前IP有效 使用代理的IP: {'http': '51.158.186.242:8811', 'https': '51.158.186.242:8811'} { "origin": "51.158.186.242" }

当前IP有效 使用代理的IP: {'http': '222.223.182.66:8000', 'https': '222.223.182.66:8000'} HTTPConnectionPool(host='222.223.182.66', port=8000): Max retries exceeded with url: http://httpbin.org/ip (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError('<urllib3.connection.HTTPConnection object at 0x0000024767514908>: Failed to establish a new connection: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。'))) 当前IP无效

以上就是使用Python验证代理IP有效性的完整实现。更多相关Python验证代理IP的内容,可参考本站往期文章或继续阅读后续教程。感谢支持!

免责声明:本文内容仅供参考,不代表本站观点。转载或引用请注明出处。

Python 相关产品和应用

全部下载
Selenium

Selenium

4.51.0 · Windows

用于浏览器自动化和 Web 测试的 Python 工具。

scikit-learn

scikit-learn

1.9.1 · Windows

常用 Python 机器学习算法与模型评估工具库。

Black

Black

26.10.1 · Windows

强调一致性的 Python 自动代码格式化工具。

Ruff

Ruff

0.17.1 · macOS

由 Rust 编写的高速 Python 代码检查和格式化工具。

pytest

pytest

9.1.1 · Windows

简洁而强大的 Python 自动化测试框架。

FastAPI

FastAPI

0.146.0 · Windows

高性能 Python API 框架,支持类型提示和自动接口文档。

Flask

Flask

3.1.3 · Windows

轻量灵活的 Python Web 应用框架。

Django

Django

6.1.2 · Windows

成熟的 Python Web 框架,适合构建内容站点和业务系统。

Spyder

Spyder

6.1.7 · Windows

面向科学计算与数据分析的 Python 集成开发环境。

SciPy

SciPy

1.18.1 · Windows

面向科学、工程和技术计算的 Python 算法库。