Python 快速验证 * 是否有效的方法实现

作者:cutercorley 时间:2022-08-12 07:22:14 

有时候,我们需要用到 * ,比如在爬虫的时候,但是得到了IP之后,可能不知道怎么验证这些IP是不是有效的,这时候我们可以使用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无效

来源:https://juejin.cn/post/6982503136631980046

标签:Python,验证, ,
0
投稿

猜你喜欢

  • Python代码调试的几种方法总结

    2022-06-14 18:21:27
  • python itchat实现微信好友头像拼接图的示例代码

    2021-04-12 18:14:56
  • 基于jQuery的自动完成插件

    2011-02-05 10:55:00
  • 用SQL语句添加删除修改字段、一些表与字段的基本操作、数据库备份等

    2011-12-01 07:53:11
  • 在Python中实现字典反转案例

    2022-12-17 02:05:46
  • Python字符串函数strip()原理及用法详解

    2021-12-01 12:08:12
  • Python hashlib加密模块常用方法解析

    2022-03-11 05:20:05
  • Django中自定义模型管理器(Manager)及方法

    2022-12-01 17:53:05
  • python之js逆向功能演示详解

    2023-09-01 13:49:29
  • 解决安装python3.7.4报错Can''t connect to HTTPS URL because the SSL module is not available

    2023-05-03 22:58:56
  • 在python下读取并展示raw格式的图片实例

    2022-07-02 18:47:56
  • Python基于动态规划算法解决01背包问题实例

    2021-01-10 21:22:26
  • python数据结构之列表和元组的详解

    2021-01-23 17:34:27
  • 常用Oracle分析函数大全

    2023-07-03 08:16:14
  • 解决使用openpyxl时遇到的坑

    2022-09-03 09:25:37
  • JS实现pasteHTML兼容ie,firefox,chrome的方法

    2023-09-21 01:55:10
  • php指定长度分割字符串str_split函数用法示例

    2023-07-08 23:25:51
  • 浅谈keras中loss与val_loss的关系

    2021-12-12 08:41:22
  • python base64库给用户名或密码加密的流程

    2021-01-30 16:30:39
  • CSS制作滑动折叠的文字效果

    2008-12-01 13:10:00
  • asp之家 网络编程 m.aspxhome.com