前言

最近在重新温习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. php pdo 获取数据转换为json

    php 查询语句获取数据,数据库有好多表现形式,如何拿出自己需要的数据 $raceSQL ='select id, race_name,race_code,content from je_race ' ...

  2. Flutter 类似viewDidAppear 的任务处理

    前言 在任务之中 ,有些实时任务比较重的需求,需要在类似 iOS viewDidAppear 里面执行数据请求任务,如:上一个页面返回pop 后执行网络请求任务.在flutter中如何实现呢?  目前 ...

  3. iOS开发系列-修改项目工程名

    当前有项目工程名为iOS,需要修改工程名为IFLY.在修改前注意备份项目 修改项目名 出现弹框,点击Rename 修改工程目录文件名 注意Tests与UITests不要删除 选中IFLY.xcodep ...

  4. 【学术篇】SDOI2009 最优图像

    又是一道辣鸡卡常数题…. luogu上有些题的时限还是有毒的… 最后也只能靠O2过掉了… 不过给我原题当时的2s我随便过给你看嘛, 哪怕评测姬慢50%都没关系的.. 贴一下codevs的截图… 你看最 ...

  5. C++ 系列:函数可变长参数

    一.基础部分 1.1 什么是可变长参数 可变长参数:顾名思义,就是函数的参数长度(数量)是可变的.比如 C 语言的 printf 系列的(格式化输入输出等)函数,都是参数可变的.下面是 printf ...

  6. day25 模块,sys, logging, json, pickle

      Python之路,Day13 = Python基础13 sys模块 sys.argv 命令行参数List,第一个元素是程序本身路径 sys.exit(n) 退出程序,正常退出时exit(0) sy ...

  7. 尚学linux课程---4、linux网络配置及linux文件

    尚学linux课程---4.linux网络配置及linux文件 一.总结 一句话总结: linux下的etc目录是配置文件的目录,所以很多的文件配置操作都可以看到它的身影:比如 init系列命名,比如 ...

  8. Visual Studio 2010 error C2065: '_In_opt_z_' : undeclared identifier 编译错误

    当用Visual Studio 2010 编译时 发生如下编译错误: 2>C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\inclu ...

  9. 第二十二篇:Spring简单定时任务

    背景:有些操作,不适合放在页面上让用户手动触发去执行,比如一些需要不断更新的数据(如统计数据)有些需要同步的数据,不需要非常实时,可以在固定的时间或者固定的频率执行同步 第一步:配置xml第二步:编写 ...

  10. 基于jdk8的格式化时间方法

    背景 jdk8之前,java使用Date表示时间,在做时间的格式化时,通常使用SimpleDateFormat,但是SimpleDateFormat是非线程安全的,在写代码时通常要将之定义为局部变量或 ...