python-爬免费ip并验证其可行性
前言
最近在重新温习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并验证其可行性的更多相关文章
- [python]爬代理ip v2.0(未完待续)
爬代理ip 所有的代码都放到了我的github上面, HTTP代理常识 HTTP代理按匿名度可分为透明代理.匿名代理和高度匿名代理. 特别感谢:勤奋的小孩 在评论中指出我文章中的错误. REMOTE_ ...
- python爬取ip地址
ip查询,异步get请求 分析接口,请求接口响应json 发现可以data中获取 result.json()['data'][0]['location'] # _*_ coding : utf-8 _ ...
- python爬取免费优质IP归属地查询接口
python爬取免费优质IP归属地查询接口 具体不表,我今天要做的工作就是: 需要将数据库中大量ip查询出起归属地 刚开始感觉好简单啊,毕竟只需要从百度找个免费接口然后来个python脚本跑一晚上就o ...
- 爬取西刺网的免费IP
在写爬虫时,经常需要切换IP,所以很有必要自已在数据维护库中维护一个IP池,这样,就可以在需用的时候随机切换IP,我的方法是爬取西刺网的免费IP,存入数据库中,然后在scrapy 工程中加入tools ...
- 无忧代理免费ip爬取(端口js加密)
起因 为了训练爬虫技能(其实主要还是js技能-),翻了可能有反爬的网站挨个摧残,现在轮到这个网站了:http://www.data5u.com/free/index.shtml 解密过程 打开网站,在 ...
- 第二篇 - python爬取免费代理
代理的作用参考https://wenda.so.com/q/1361531401066511?src=140 免费代理很多,但也有很多不可用,所以我们可以用程序对其进行筛选.以能否访问百度为例. 1. ...
- 爬取快代理的免费IP并测试
各大免费IP的网站的反爬手段往往是封掉在一定时间内访问过于频繁的IP,因此在爬取的时候需要设定一定的时间间隔,不过说实话,免费代理很多时候基本都不能用,可能一千个下来只有十几个可以用,而且几分钟之后估 ...
- python 单例模式获取IP代理
python 单例模式获取IP代理 tags:python python单例模式 python获取ip代理 引言:最近在学习python,先说一下我学Python得原因,一个是因为它足够好用,完成同样 ...
- Python获取免费的可用代理
Python获取免费的可用代理 在使用爬虫多次爬取同一站点时,常常会被站点的ip反爬虫机制给禁掉,这时就能够通过使用代理来解决.眼下网上有非常多提供最新免费代理列表的站点.这些列表里非常多的代理主机是 ...
随机推荐
- Dribbble 流行的配色风格是什么?
Dribbble 是众所周知的设计社群网站,在网站中有许多人分享设计作品,互相交流或从其他设计获取灵感.当然也有不少网站应运而生,例如 Freebbble 可免费下载 Dribbble 数千种设计素材 ...
- nginx 知识
nginx如何实现高并发? 启动nginx服务器后,输入 ps -ef |grep nginx,会发现nginx有一个master进程 和若干个worker进程, 这些worker进程是平等的,都是被 ...
- 【bug】使用element-ui遇到在IE浏览器中点击enter会回到登录页
1.点击el-input框,会回到登录页(IE浏览器) 外层是el-table/el-form/el-input 添加可以解决 <el-form onSubmit="return fa ...
- 数据库MySQL--连接查询
例子文件1:https://files.cnblogs.com/files/Vera-y/myemployees.zip 例子文件2:https://files-cdn.cnblogs.com/fil ...
- python中的 += 语法的使用
python中有个缩略的写法,如下 a = a +1 等同于 a +=1 发现了一个有趣之处,+=的写法中间不能有空格,否则报错,测试如下 Python 3.7.1 (v3.7.1:260ec2c36 ...
- python 基本常用数据类型
#字典类型 result={1:2222,2:2221111}; result.items();#获取字典中所有元素 result.keys();#获取字典的key result.values();# ...
- 2018-2019-2 20175323 实验一《Java开发环境的熟悉》实验报告
java开发环境的熟悉-1 java开发环境的熟悉-2 下载IDEA和破解的过程我参考了https://blog.csdn.net/shengshengshiwo/article/details/79 ...
- Hexo 博客图片添加至图床---腾讯云COS图床使用。
个人博客:https://mmmmmm.me 源码:https://github.com/dataiyangu/dataiyangu.github.io 腾讯云官网 登录注册 创建存储桶 进入上面的存 ...
- Mysql优化系列之数据类型优化
本篇是优化系列的第一篇:数据类型 为了不产生赘述,尽量用简洁的语言来描述. 在选择数据类型之前,首先要知道几个原则: 更小的通常更好 尽量使用可以正确存储数据的最小数据类型.更小的数据类型意味着更快, ...
- 微信小程序 tabBar模板
tabBar导航栏 小程序tabBar,我们可以通过app.json进行配置,可以放置于顶部或者底部,用于不同功能页面的切换,挺好的... 但,,,貌似不能让动态修改tabBar(需求:通过switc ...