Python 快速验证代理IP是否有效
有时候,我们需要用到代理IP,比如在爬虫的时候,但是得到了IP之后,可能不知道怎么验证这些IP是不是有效的,这时候我们可以使用Python携带该IP来模拟访问某一个网站,如果多次未成功访问,则说明这个代理是无效的。
代码如下:
很多人学习python,不知道从何学起。
很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手。
很多已经做案例的人,却不知道如何去学习更加高深的知识。
那么针对这三类人,我给大家提供一个好的学习平台,免费领取视频教程,电子书籍,以及课程的源代码!
QQ群:101677771
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是否有效的更多相关文章
- c# 快速验证代理IP是否有用
public void yanzhen(string str, int port) { WebProxy proxyObject = new WebProxy(str, port);//str为IP地 ...
- Python 爬虫的代理 IP 设置方法汇总
本文转载自:Python 爬虫的代理 IP 设置方法汇总 https://www.makcyun.top/web_scraping_withpython15.html 需要学习的地方:如何在爬虫中使用 ...
- 【原创】验证代理IP是否有用
/// <summary> /// 验证代理IP是否有用 /// </summary> /// <param name="ip">IP地址< ...
- python爬虫构建代理ip池抓取数据库的示例代码
爬虫的小伙伴,肯定经常遇到ip被封的情况,而现在网络上的代理ip免费的已经很难找了,那么现在就用python的requests库从爬取代理ip,创建一个ip代理池,以备使用. 本代码包括ip的爬取,检 ...
- 如何验证代理ip的正确性
python requests 使用代理的话: proxy_list = ["aabbcc.aabbcc.169.aabbcc:8080", ] ip_p = random.cho ...
- PHP防止网页快速刷新+代理ip访问
前几天网站收到了一些CC攻击,比较郁闷...这里分享一下,防止网页自动刷新的方法以及阻止代理IP访问网站的方法,代码是分开的,两个功能,需要那个用那个,可以自定义时间间隔,这个代码不止可以防CC攻击, ...
- python多线程建立代理ip池
之前有写过用单线程建立代理ip池,但是大家很快就会发现,用单线程来一个个测试代理ip实在是太慢了,跑一次要很久才能结束,完全无法忍受.所以这篇文章就是换用多线程来建立ip池,会比用单线程快很多.之所以 ...
- 验证代理IP
##author:wuhao#import urllib.requestfrom http import cookiejar import xlrd import threading #有效的代理,可 ...
- Java 验证代理ip
原文地址:http://www.cnblogs.com/junrong624/p/5416503.html 1 import java.io.IOException; import java.io.I ...
随机推荐
- 不使用spring-boot-starter-parent作为依赖parent
背景环境 在某些情况下由于某些原因,我们的项目不能使用spring-boot-starter-parent作为<parent>依赖,一定要有自己的<parent>,但同时还希望 ...
- 12、Java 正则表达式
简介 用来描述或者匹配一系列符合某个语句规则的字符串 正则表达式定义了字符串的模式. 正则表达式可以用来搜索.编辑或处理文本. 正则表达式并不仅限于某一种语言,但是在每种语言中有细微的差别. 一.正则 ...
- centos 8 安装和网络配置
centos 8 系统安装 系统安装步骤 启动服务器之后选择 Install CentOs Linux 8 选择语言然后下一步 配置 磁盘(Installation Destir) 这里选择默认配置 ...
- image classification backbone 汇总分析
下面是一个list,可以详细看一下 image_classification = [['name','top1_acc','top5_acc','size'],['FixEfficientNet-L2 ...
- CSS 定位总结
目录 元素显示模式 元素模式 元素显示模式转换 CSS定位机制 静态定位static 相对定位relative 绝对定位absolute 固定定位fixed 粘性定位sticky 定位小结一图流 CS ...
- ZoneJS 的原理与应用
目录 序言 Zone 是什么 ZoneJS 的原理 ZoneJS 的应用场景 参考 1. 序言 ZoneJS 是 Angular 团队受到 Dart 的 Zone 的启发,为 Angular v2 及 ...
- JavaScript npm/nrm 切换安装依赖的镜像源
nrm: npm registry manager npm 镜像源管理工具 安装nrm npm install -g nrm 查看所有的镜像源 nrm ls # nrm ls npm -------- ...
- JavaScript Object初始化的不同方式
不带原型的对象,纯对象 const plaintObject = Object.create(null) 带原型的对象 const originObject = new Object()
- ubuntu升级已安装git版本
# To get the very latest version of git, you need to add the PPA (Personal Package Archive) from the ...
- 免费深度学习GPU,Google Yes!
深度学习越加火热,但是,很多实验室并没有配套的硬件设备,让贫穷的学生党头大 经过网上大量的搜罗,我整理了适合学生党的深度学习解决方案.利用Colab + Kaggle两大免费的GPU环境,让深度学习变 ...