前言

最近在重新温习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. java面试官如何面试别人

                                                                                      java面试官如何面试别人(一) j ...

  2. 5步减小你的CSS样式表

    第一步:学会如何组合选择器 什么是选择器?如果你还不知道什么叫做”选择器”,你可以先参考一下w3schools.com的CSS语法概述. 未优化的CSS代码在下面的图例中,你会看到来自三个不同选择器的 ...

  3. JS对象 提取指定数目的字符substr() substr() 方法从字符串中提取从 startPos位置开始的指定数目的字符串。

    提取指定数目的字符substr() substr() 方法从字符串中提取从 startPos位置开始的指定数目的字符串. 语法: stringObject.substr(startPos,length ...

  4. gerp 用法

    1. 提取/etc/ansible/hosts 文件中包括star的行,且包含数字,不以[为开头的行 grep 'star' /etc/ansible/hosts | grep  '[[:digit: ...

  5. SQL链接EXCEL操作

    Sub CopyData_5() Set Cnn = CreateObject("ADODB.Connection")With Cnn.Provider = "micro ...

  6. MySQL sql_mode 说明(及处理一起sql_mode引发的问题)

    转自:https://segmentfault.com/a/1190000005936172 1. MySQL 莫名变成了 Strict SQL Mode 最近测试组那边反应数据库部分写入失败,app ...

  7. apache 80 端口 反向代理 tomcat 8080端口

    最近有个jsp的项目要放到服务上,但服务器上已经有了XAMPP(apache + mysql + php), 已占用了80端口.但http默认是访问80端口的. 先把tomcat 环境搭建起来, 发现 ...

  8. Java 基础 - CLASSPATH 到底是什么

    关于JAVA项目中CLASSPATH路径详解 https://www.cnblogs.com/hibou/p/8324276.html java项目中的classpath到底是什么 https://s ...

  9. clientHeight / scrollHeight / offsetHeight 等属性的区别图

    网页(内容)可见区域宽:document.body.clientWidth 网页(内容)可见区域高:document.body.clientHeight 即页面浏览器中可以看到内容的这个区域的高度,一 ...

  10. VS2010-MFC(常用控件:编辑框Edit Control)

    转自:http://www.jizhuomi.com/software/181.html 编辑框(Edit Control)是一种很常用的控件,我们可以在编辑框中输入并编辑文本.在前面加法计算器的例子 ...