前言

最近在重新温习python基础-正则,感觉正则很强大,不过有点枯燥,想着,就去应用正则,找点有趣的事玩玩

00xx01---代理IP

有好多免费的ip,不过一个一个保存太难了,也不可能,还是用我们的python爬取吧

00xx02---正则提取ip

 import requests
import re #防反爬
headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36" } url = "https://www.xicidaili.com/nn/1" response = requests.get(url,headers=headers)
# print(response.text) html = response.text
#print(html) #re.S忽略换行的干扰
ips = re.findall("<td>(\d+\.\d+\.\d+\.\d+)</td>",html,re.S)
ports = re.findall(("<td>(\d+)</td>"),html,re.S)
print(ips)
print(ports)

00xx03---拼接IP和端口

 import requests
import re #防反爬
headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36" } url = "https://www.xicidaili.com/nn/1" response = requests.get(url,headers=headers)
# print(response.text) html = response.text
# print(html) #re.S忽略换行的干扰
ips = re.findall("<td>(\d+\.\d+\.\d+\.\d+)</td>",html,re.S)
ports = re.findall(("<td>(\d+)</td>"),html,re.S)
#print(ips)
#print(ports)
for ip in zip(ips,ports ): #提取拼接ip和端口
print(ip)

00xx03---验证IP可行性

思路:带着ip和端口去访问一个网站,百度就可以

 import requests
import re headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36" }
for i in range(1,1000):
#网址
url = "https://www.xicidaili.com/nn/{}".format(i) response = requests.get(url,headers=headers)
# print(response.text) html = response.text #re.S忽略换行的干扰
ips = re.findall("<td>(\d+\.\d+\.\d+\.\d+)</td>",html,re.S)
ports = re.findall(("<td>(\d+)</td>"),html,re.S)
# print(ips)
# print(ports)
for ip in zip(ips,ports ): #提取拼接ip和端口
proxies = {
"http":"http://" + ip[0] + ":" + ip[1],
"https":"http://" + ip[0] + ":" + ip[1]
}
try:
res = requests.get("http://www.baidu.com",proxies=proxies,timeout = 3) #访问网站等待3s没有反应,自动断开
print(ip,"能使用")
with open("ip.text",mode="a+") as f:
f.write(":".join(ip)) #写入ip.text文本
f.write("\n") #换行
except Exception as e: #捕捉错误异常
print(ip,"不能使用")

00xx04---写入文本

 import requests
import re #防反爬
headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36" } url = "https://www.xicidaili.com/nn/1" response = requests.get(url,headers=headers)
# print(response.text) html = response.text
# print(html) #re.S忽略换行的干扰
ips = re.findall("<td>(\d+\.\d+\.\d+\.\d+)</td>",html,re.S)
ports = re.findall(("<td>(\d+)</td>"),html,re.S)
#print(ips)
#print(ports)
for ip in zip(ips,ports ): #提取拼接ip和端口
print(ip)
proxies = {
"http":"http://" + ip[0] + ":" + ip[1],
"https":"http://" + ip[0] + ":" + ip[1]
}
try:
res = requests.get("http://www.baidu.com",proxies=proxies,timeout = 3) #访问网站等待3s没有反应,自动断开
print(ip,"能使用")
with open("ip.text",mode="a+") as f:
f.write(":".join(ip)) #写入ip.text文本
f.write("\n") #换行
except Exception as e: #捕捉错误异常
print(ip,"不能使用")

爬了一页,才几个能用,有3000多页,不可能手动的

00xx05---批量爬

 #!/usr/bin/env python3
# coding:utf-8
# 2019/11/18 22:38
#lanxing
import requests
import re #防反爬
headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36" }
for i in range(1,3000): #爬3000个网页
#网站
url = "https://www.xicidaili.com/nn/{}".format(i) response = requests.get(url,headers=headers)
# print(response.text) html = response.text
# print(html) #re.S忽略换行的干扰
ips = re.findall("<td>(\d+\.\d+\.\d+\.\d+)</td>",html,re.S)
ports = re.findall(("<td>(\d+)</td>"),html,re.S)
#print(ips)
#print(ports)
for ip in zip(ips,ports ): #提取拼接ip和端口
print(ip)
proxies = {
"http":"http://" + ip[0] + ":" + ip[1],
"https":"http://" + ip[0] + ":" + ip[1]
}
try:
res = requests.get("http://www.baidu.com",proxies=proxies,timeout = 3) #访问网站等待3s没有反应,自动断开
print(ip,"能使用")
with open("ip.text",mode="a+") as f:
f.write(":".join(ip)) #写入ip.text文本
f.write("\n") #换行
except Exception as e: #捕捉错误异常
print(ip,"不能使用")

00xx06---最后

哈哈,感觉爬的速度太慢了,毕竟是单线程,如果要快速爬,可以试试用多线程爬取,

以后再补充完善代码吧

python-爬免费ip并验证其可行性的更多相关文章

  1. [python]爬代理ip v2.0(未完待续)

    爬代理ip 所有的代码都放到了我的github上面, HTTP代理常识 HTTP代理按匿名度可分为透明代理.匿名代理和高度匿名代理. 特别感谢:勤奋的小孩 在评论中指出我文章中的错误. REMOTE_ ...

  2. python爬取ip地址

    ip查询,异步get请求 分析接口,请求接口响应json 发现可以data中获取 result.json()['data'][0]['location'] # _*_ coding : utf-8 _ ...

  3. python爬取免费优质IP归属地查询接口

    python爬取免费优质IP归属地查询接口 具体不表,我今天要做的工作就是: 需要将数据库中大量ip查询出起归属地 刚开始感觉好简单啊,毕竟只需要从百度找个免费接口然后来个python脚本跑一晚上就o ...

  4. 爬取西刺网的免费IP

    在写爬虫时,经常需要切换IP,所以很有必要自已在数据维护库中维护一个IP池,这样,就可以在需用的时候随机切换IP,我的方法是爬取西刺网的免费IP,存入数据库中,然后在scrapy 工程中加入tools ...

  5. 无忧代理免费ip爬取(端口js加密)

    起因 为了训练爬虫技能(其实主要还是js技能-),翻了可能有反爬的网站挨个摧残,现在轮到这个网站了:http://www.data5u.com/free/index.shtml 解密过程 打开网站,在 ...

  6. 第二篇 - python爬取免费代理

    代理的作用参考https://wenda.so.com/q/1361531401066511?src=140 免费代理很多,但也有很多不可用,所以我们可以用程序对其进行筛选.以能否访问百度为例. 1. ...

  7. 爬取快代理的免费IP并测试

    各大免费IP的网站的反爬手段往往是封掉在一定时间内访问过于频繁的IP,因此在爬取的时候需要设定一定的时间间隔,不过说实话,免费代理很多时候基本都不能用,可能一千个下来只有十几个可以用,而且几分钟之后估 ...

  8. python 单例模式获取IP代理

    python 单例模式获取IP代理 tags:python python单例模式 python获取ip代理 引言:最近在学习python,先说一下我学Python得原因,一个是因为它足够好用,完成同样 ...

  9. Python获取免费的可用代理

    Python获取免费的可用代理 在使用爬虫多次爬取同一站点时,常常会被站点的ip反爬虫机制给禁掉,这时就能够通过使用代理来解决.眼下网上有非常多提供最新免费代理列表的站点.这些列表里非常多的代理主机是 ...

随机推荐

  1. batch、随机、Mini-batch梯度下降

    batch梯度下降: 对所有m个训练样本执行一次梯度下降,每一次迭代时间较长: Cost function 总是向减小的方向下降. 随机梯度下降: 对每一个训练样本执行一次梯度下降,但是丢失了向量化带 ...

  2. mac NTFS 关于错误-36,rm Input/output error

    当传输文件时出现Mac错误代码36 当我向一个U盘或存储卡里传输文件时,出现了Mac Error Code36,我该如何解决? Mac,传输文件,错误代码36,U盘,卡片 Mac OS X Snow ...

  3. VS环境下,DEV插件的ComboBoxEdit控件最简单的数据源绑定和获取方法

    使用 ComboBoxEdit 控件绑定key/value值: 因为 ComboBoxEdit 没有 DataSource 属性,所以不能直接绑定数据源,只能一项一项的添加. 首先创建一个类ListI ...

  4. UOJ67 新年的毒瘤【Tarjan,割点】

    Online Judge:#uoj 67 Label:Tarjan,割点,细节 题目描述 辞旧迎新之际,喜羊羊正在打理羊村的绿化带,然后他发现了一棵长着毒瘤的树.这个长着毒瘤的树可以用\(n\)个结点 ...

  5. Android开发 自定义View_白色圆型涟漪动画View

    代码: import android.animation.ValueAnimator; import android.content.Context; import android.graphics. ...

  6. Docker的镜像 导出导入

    查看当前已经安装的镜像 vagrant@vagrant:~$ sudo docker images REPOSITORY TAG IMAGE ID CREATED SIZE mysql 5.7.22 ...

  7. Erlang学习记录:相关工具和文档

    在线工具和文档 网址 说明 OTP Reference Page Index 内置模块查询 Erlang/OTP Applications N Kernel Reference Manual 入门官方 ...

  8. String str = new String("abc"),这段代码一共生成了几个String对象?为什么?

    String str = new String("abc")创建了俩个对象,首先为创建一个String对象"abc",然后在调用String类的构造方法时 pu ...

  9. Photoshop基本操作

    PS 工具是我们使用频率比较高的软件之一, 我们学习PS目的不是为了设计海报做电商和UI的,而是要求: 会简单的抠图 会简单的修改PSD效果图 熟练的切图 能和网站美工美眉有共同话题..... Pho ...

  10. thinkphp 运算符

    我们可以对模板输出使用运算符,包括对“+”“ –” “*” “/”和“%”的支持. 大理石平台厂家 例如: 运算符 使用示例 + {$a+$b} - {$a-$b} * {$a*$b} / {$a/$ ...