关于检测代理ip能不能用
本帖最后由 阿匠 于 2020-9-27 13:04 编辑import requests
url='https://www.baidu.com'
headers={'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36 Edg/85.0.564.51'}
proxy={'http':'123.55.101.73:9999'}
response=requests.get(url=url,headers=headers,proxies=proxy)
if response.status_code!=200:
print('bad')
else:
print('good')
r=requests.get(url='http://jandan.net/',headers=headers,proxies=proxy)
html=r.text
print(html)
ps:
这个ip地址是网上找的http 类型的高匿ip,后面用来打开的网址也是http类型的
此代码会打印good 然后报错
Traceback (most recent call last):
File "C:\Users\wmx\Desktop\testip.py", line 16, in <module>
r=requests.get(url='http://jandan.net/',headers=headers,proxies=proxy)
File "D:\python\lib\site-packages\requests\api.py", line 76, in get
return request('get', url, params=params, **kwargs)
File "D:\python\lib\site-packages\requests\api.py", line 61, in request
return session.request(method=method, url=url, **kwargs)
File "D:\python\lib\site-packages\requests\sessions.py", line 530, in request
resp = self.send(prep, **send_kwargs)
File "D:\python\lib\site-packages\requests\sessions.py", line 643, in send
r = adapter.send(request, **kwargs)
File "D:\python\lib\site-packages\requests\adapters.py", line 510, in send
raise ProxyError(e, request=request)
requests.exceptions.ProxyError: HTTPConnectionPool(host='123.55.101.73', port=9999): Max retries exceeded with url: http://jandan.net/ (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError('<urllib3.connection.HTTPConnection object at 0x0000000003A414A8>: Failed to establish a new connection: 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。')))
用这样检测,你要检测网址,需要直接返回你的IP地址,看看是不是代理IP地址,一致的有效
免费的IP很多都是无效的,有个2、3个有效就不错了
def Check_httpip(self,new_list):
'''代理IP地址(高匿)'''
new_list_active=[]
for proxy in new_list:
print(f'开始检测{proxy}...')
if 'http' in proxy.keys() :
'''http://icanhazip.com会返回当前的IP地址'''
try:
response = requests.get(url='http://icanhazip.com', headers=self.headers, proxies=proxy,timeout=3)
print(f'返回结果:{response.text}')
new_list_active.append(proxy)
time.sleep(1)
except:
print(f'{proxy}未返回结果,无效...')
elif 'https' in proxy.keys() :
try:
response = requests.get(url='https://foundation.youdao.com/ip/ipinfo', headers=self.headers, proxies=proxy,timeout=3)
print(f'返回结果:{response.text}')
new_list_active.append(proxy)
time.sleep(1)
except:
print(f'{proxy}未返回结果,无效...')
print(new_list_active)
if len(new_list_active)>0:
self.Save_To_Txt(new_list_active)
else:
print('无有效免费代理IP地址') 这种通过的代理不能去访问百度网
冰西瓜 发表于 2020-10-14 18:58
这种通过的代理不能去访问百度网
不是不能,要看你用的什么代理吧,http,还是https
页:
[1]