前言

最近在重新温习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. centos7.3更换ssh默认登陆端口

    说明:本方法目前通用于7.1-7.3 vim /etc/ssh/sshd_config 找到Port 22下面添加一行:Port 12345保存退出. systemctl restart sshd.s ...

  2. shell 命令 用户管理

     1. 查看保存用户相关信息的文件 [ cat /etc/passwd ]  [linux    :    x    :   1000  :   1000   :   linux,,,   :    ...

  3. 一个有关group by的错误

    事例:查询有奖金的每个部门的部门名和部门的领导编号和该部门的最低工资 SELECT department_name,MIN(salary),departments.manager_idFROM dep ...

  4. PKUSC加油加油加油!

    一句话,把学过的掌握的甚至还未掌握的,都用上吧! 1.题目不要再再再看错了!在纸上记下关键字. 2.记得有预处理这个东西可以降低复杂度! 3.仔细阅读数据范围,取值范围的0要注意! 4.不要每次像开新 ...

  5. U Must Know The .Net --7

    关键字 1 new 创建对象/调用构造函数 隐藏基类成员 new()约束,表明泛型类声明中的任何参数都必须有公共无参构造函数 new 实现多态 1.1 new class:分配内存,调用构造函数实例化 ...

  6. MySQL Download

    { http://www.wampserver.com/#wampserver-64-bits-php-5-6-25-php-7 }

  7. 强连通图缩点——cf999E

    问题转换成缩点求度数为0的点的个数,s点所在联通块作额外处理 缩点写的很烂调了一早上.. #include<bits/stdc++.h> #include<vector> us ...

  8. SpringCloud及其五大常用组件之Feign、Ribbon和Hystrix

    1.Feign 我们已经将Eureka和Zuul开发完毕,而且上面注册了两个微服务,现在我们实现两个微服务之间的调用. String baseUrl = "http://127.0.0.1: ...

  9. gradle配置全局仓库

    1.在系统环境变量中配置: GRADLE_USER_HOME=D:\gradleRepository 2.在配置的路径中,增加文件init.gradle allprojects{ repositori ...

  10. 后缀自动机XJ

    后缀自动机初探(xiajiang) 后缀树\((Suffix Tree)\) 对于一个字符串,把它的所有后缀插入到\(Trie\)中就是一个后缀树. 当然字母存在边上,最终的点可以用一个特殊符号如:\ ...