Python 快速驗(yàn)證代理IP是否有效的方法實(shí)現(xiàn)
有時(shí)候,我們需要用到代理IP,比如在爬蟲的時(shí)候,但是得到了IP之后,可能不知道怎么驗(yàn)證這些IP是不是有效的,這時(shí)候我們可以使用Python攜帶該IP來(lái)模擬訪問某一個(gè)網(wǎng)站,如果多次未成功訪問,則說(shuō)明這個(gè)代理是無(wú)效的。
代碼如下:
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('當(dāng)前IP有效')
time.sleep(2)
except Exception as e:
print(e.args[0])
print('當(dāng)前IP無(wú)效')
continue
運(yùn)行結(jié)果如下:
使用代理的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] 由于連接方在一段時(shí)間后沒有正確答復(fù)或連接的主機(jī)沒有反應(yīng),連接嘗試失敗。')))
當(dāng)前IP無(wú)效
使用代理的IP: {'http': '51.158.186.242:8811', 'https': '51.158.186.242:8811'}
{
"origin": "51.158.186.242"
}當(dāng)前IP有效
使用代理的IP: {'http': '222.223.182.66:8000', 'https': '222.223.182.66:8000'}
{
"origin": "139.202.62.84, 222.223.182.66"
}當(dāng)前IP有效
使用代理的IP: {'http': '51.158.186.242:8811', 'https': '51.158.186.242:8811'}
{
"origin": "51.158.186.242"
}當(dāng)前IP有效
使用代理的IP: {'http': '51.158.186.242:8811', 'https': '51.158.186.242:8811'}
{
"origin": "51.158.186.242"
}當(dāng)前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] 由于連接方在一段時(shí)間后沒有正確答復(fù)或連接的主機(jī)沒有反應(yīng),連接嘗試失敗。')))
當(dāng)前IP無(wú)效
使用代理的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] 由于連接方在一段時(shí)間后沒有正確答復(fù)或連接的主機(jī)沒有反應(yīng),連接嘗試失敗。')))
當(dāng)前IP無(wú)效
使用代理的IP: {'http': '51.158.186.242:8811', 'https': '51.158.186.242:8811'}
{
"origin": "51.158.186.242"
}當(dāng)前IP有效
使用代理的IP: {'http': '51.158.186.242:8811', 'https': '51.158.186.242:8811'}
{
"origin": "51.158.186.242"
}當(dāng)前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] 由于連接方在一段時(shí)間后沒有正確答復(fù)或連接的主機(jī)沒有反應(yīng),連接嘗試失敗。')))
當(dāng)前IP無(wú)效
到此這篇關(guān)于Python 快速驗(yàn)證代理IP是否有效的方法實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Python 驗(yàn)證代理IP是否有效內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python button選取本地圖片并顯示的實(shí)例
今天小編就為大家分享一篇Python button選取本地圖片并顯示的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-06-06
在scrapy中使用phantomJS實(shí)現(xiàn)異步爬取的方法
今天小編就為大家分享一篇在scrapy中使用phantomJS實(shí)現(xiàn)異步爬取的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-12-12
python爬取淘寶商品詳情頁(yè)數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了python爬取淘寶商品詳情頁(yè)數(shù)據(jù)的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02
Python?NumPy科學(xué)計(jì)算庫(kù)的高級(jí)應(yīng)用
這篇文章主要為大家介紹了Python?NumPy科學(xué)計(jì)算庫(kù)的高級(jí)應(yīng)用深入詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
python機(jī)器學(xué)習(xí)高數(shù)篇之泰勒公式
這篇文章主要介紹了python機(jī)器學(xué)習(xí)高數(shù)篇之函數(shù)極限和導(dǎo)數(shù),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08
Python圖像處理庫(kù)crop()函數(shù)?thumbnail方法使用詳解
這篇文章主要為大家介紹了Python圖像處理庫(kù)crop()函數(shù)?thumbnail方法使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
Python基于模塊Paramiko實(shí)現(xiàn)SSHv2協(xié)議
這篇文章主要介紹了Python基于模塊Paramiko實(shí)現(xiàn)SSHv2協(xié)議,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04

