有时候,我们需要用到代理IP,比如在爬虫的时候,但是得到了IP之后,可能不知道怎么验证这些IP是不是有效的,这时候我们可以使用Python携带该IP来模拟访问某一个网站,如果多次未成功访问,则说明这个代理是无效的。 代码如下:
1import requests 2import random 3import time 4 5http_ip = [ 6 '118.163.13.200:8080', 7 '222.223.182.66:8000', 8 '51.158.186.242:8811', 9 '171.37.79.129:9797', 10 '139.255.123.194:4550' 11] 12 13for i in range(10): 14 try: 15 ip_proxy = random.choice(http_ip) 16 proxy_ip = { 17 'http': ip_proxy, 18 'https': ip_proxy, 19 } 20 print('使用代理的IP:', proxy_ip) 21 response = requests.get("http://httpbin.org/ip", proxies=proxy_ip).text 22 print(response) 23 print('当前IP有效') 24 time.sleep(2) 25 except Exception as e: 26 print(e.args[0]) 27 print('当前IP无效') 28 continue 29
运行结果如下:
1使用代理的IP: {'http': '118.163.13.200:8080', 'https': '118.163.13.200:8080'} 2HTTPConnectionPool(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] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。'))) 3当前IP无效 4使用代理的IP: {'http': '51.158.186.242:8811', 'https': '51.158.186.242:8811'} 5{ 6 "origin": "51.158.186.242" 7} 8 9当前IP有效 10使用代理的IP: {'http': '222.223.182.66:8000', 'https': '222.223.182.66:8000'} 11{ 12 "origin": "139.202.62.84, 222.223.182.66" 13} 14 15当前IP有效 16使用代理的IP: {'http': '51.158.186.242:8811', 'https': '51.158.186.242:8811'} 17{ 18 "origin": "51.158.186.242" 19} 20 21当前IP有效 22使用代理的IP: {'http': '51.158.186.242:8811', 'https': '51.158.186.242:8811'} 23{ 24 "origin": "51.158.186.242" 25} 26 27当前IP有效 28使用代理的IP: {'http': '222.223.182.66:8000', 'https': '222.223.182.66:8000'} 29HTTPConnectionPool(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] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。'))) 30当前IP无效 31使用代理的IP: {'http': '139.255.123.194:4550', 'https': '139.255.123.194:4550'} 32HTTPConnectionPool(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] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。'))) 33当前IP无效 34使用代理的IP: {'http': '51.158.186.242:8811', 'https': '51.158.186.242:8811'} 35{ 36 "origin": "51.158.186.242" 37} 38 39当前IP有效 40使用代理的IP: {'http': '51.158.186.242:8811', 'https': '51.158.186.242:8811'} 41{ 42 "origin": "51.158.186.242" 43} 44 45当前IP有效 46使用代理的IP: {'http': '222.223.182.66:8000', 'https': '222.223.182.66:8000'} 47HTTPConnectionPool(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] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。'))) 48当前IP无效 49
本文原文首发来自博客专栏Python实战,由本人转发至https://www.helloworld.net/p/GGguV7tALU4J,其他平台均属侵权,可点击https://blog.csdn.net/CUFEECR/article/details/105475019查看原文,也可点击https://blog.csdn.net/CUFEECR浏览更多优质原创内容。
