自动收集有效IP代理

#需要的外部依赖包requests和lxml
#自动获取的代理ip数据保存为”IP代理池.txt“
#read_ip函数用于提取”IP代理池.txt“的数据返回类型为列表
from lxml import etree
import requests
import datetime
not_alive=0
is_alive=0
def rm_symbol(c1):
new_c1=c1.replace('\n','').replace('\t','')
return new_c1
#------------代理ip网站数据爬取(ip+空格+port形式)------------
def get_89ip_data():
for i in range(1, 16):
url = 'http://www.89ip.cn/index_{}.html'.format(i)
res = requests.get(url)
res.encoding = 'utf-8'
html=etree.HTML(res.text)
ipdress=html.xpath('//table[@class="layui-table"]/tbody/tr/td[1]/text()')
port=html.xpath('//table[@class="layui-table"]/tbody/tr/td[2]/text()')
ipdress=list(map(rm_symbol,ipdress))
port=list(map(rm_symbol,port))
data=list(zip(ipdress,port))
for i in range(len(data)):
ip_is_alive(data[i][0]+' '+data[i][1])
#----------------------------------------

#ip存活检测
def ip_is_alive(ip_port):
import telnetlib
global is_alive
global not_alive
ip=ip_port.split(' ')[0]
port=ip_port.split(' ')[-1]
try:
tn = telnetlib.Telnet(ip, port=port,timeout=2)
except:
print('[-] ip:{}:{}'.format(ip,port))
not_alive+=1
else:
print('[+] ip:{}:{}'.format(ip,port))
is_alive+=1
with open('IP代理池.txt','a') as f:
f.write(ip+':'+port+'\n')

#提取IP池的ip 以列表形式返回
def read_ip(dress):
with open(dress,'r') as f:
ip_port=f.read().split('\n')[1:]
for i in ip_port:
if i=='':
ip_port.remove(i)
return ip_port

#检测过程
def check_ip():
print('------------IP检测开始------------')
print('[+]代表ip有效\n[-]代表ip无效\n')
with open('IP代理池.txt', 'w') as f:
f.write('------------自动获取 IP代理池-----------\n')
get_89ip_data()
with open('IP代理池.txt', 'a') as f:
f.write('------更新时间:{}-----\n'.format(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
print("---总共检测{}个IP 有效IP:{}个 无效IP:{}个---".format(is_alive+not_alive,is_alive,not_alive))

check_ip()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

---------------------

自动收集有效IP代理的更多相关文章

  1. python获取ip代理列表爬虫

    最近练习写爬虫,本来爬几张mm图做测试,可是爬到几十张的时候就会返回403错误,这是被网站服务器发现了,把我给屏蔽了. 因此需要使用代理IP.为了方便以后使用,我打算先写一个自动爬取ip代理的爬虫,正 ...

  2. 开源IP代理池续——整体重构

    开源IP代理池 继上一篇开源项目IPProxys的使用之后,大家在github,我的公众号和博客上提出了很多建议.经过两周时间的努力,基本完成了开源IP代理池IPProxyPool的重构任务,业余时间 ...

  3. 被IP代理网站屏蔽了,真是跪了

    被IP代理网站http://www.xicidaili.com/nn/屏蔽了,真是跪了 T T

  4. Linux IP代理筛选系统(shell+proxy)

    代理的用途 其实,除了抓取国外网页需要用到IP代理外,还有很多场景会用到代理: 通过代理访问一些国外网站,绕过被某国防火墙过滤掉的网站 使用教育网的代理服务器,可以访问到大学或科研院所的内部网站资源 ...

  5. 随机IP代理

    第一个例子就设置了一个代理IP,也是不靠谱的,最好的方式就是多设置几个,如第二个例子,通过http://www.youdaili.net/Daili/你可以找到很多代理IP, 抓取国内网站时尽量选取中 ...

  6. 记一次企业级爬虫系统升级改造(六):基于Redis实现免费的IP代理池

    前言: 首先表示抱歉,春节后一直较忙,未及时更新该系列文章. 近期,由于监控的站源越来越多,就偶有站源做了反爬机制,造成我们的SupportYun系统小爬虫服务时常被封IP,不能进行数据采集. 这时候 ...

  7. 爬取西刺ip代理池

    好久没更新博客啦~,今天来更新一篇利用爬虫爬取西刺的代理池的小代码 先说下需求,我们都是用python写一段小代码去爬取自己所需要的信息,这是可取的,但是,有一些网站呢,对我们的网络爬虫做了一些限制, ...

  8. scrapy_随机ip代理池

    什么是ip代理? 我们电脑访问网站,其实是访问远程的服务器,通过ip地址识别是那个机器访问了服务器,服务器就知道数据该返回给哪台机器,我们生活中所用的网络是局域网,ip是运营商随机分配的,是一种直接访 ...

  9. 反反爬虫 IP代理

    0x01 前言 一般而言,抓取稍微正规一点的网站,都会有反爬虫的制约.反爬虫主要有以下几种方式: 通过UA判断.这是最低级的判断,一般反爬虫不会用这个做唯一判断,因为反反爬虫非常容易,直接随机UA即可 ...

随机推荐

  1. Pollard Rho算法浅谈

    Pollard Rho介绍 Pollard Rho算法是Pollard[1]在1975年[2]发明的一种将大整数因数分解的算法 其中Pollard来源于发明者Pollard的姓,Rho则来自内部伪随机 ...

  2. Codeforces Round #285 (Div. 2)C. Misha and Forest(拓扑排序)

    传送门 Description Let's define a forest as a non-directed acyclic graph (also without loops and parall ...

  3. 面向JVM的应用程序的项目结构

    对于Maven所用的项目结构,称为Maven标准的目录结构,不包含git 一.一个典型的源代码结构: / [project-name] README.txt LICENSE.txt pom.xml / ...

  4. JavaScript.Remove

    Array.prototype.remove = function (from, to) {     var rest = this.slice((to || from) + 1 || this.le ...

  5. SpringMvc.xml

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  6. Linux系统配置Java开发基本环境

    jdk安装一.用yum安装jdk1.查看yum库都有哪些jdk版本yum search java|grep jdk2.选择版本安装yum install java-1.8.0-openjdk(/usr ...

  7. Java并发编程:线程的同步

    Java并发编程:线程的同步 */--> code {color: #FF0000} pre.src {background-color: #002b36; color: #839496;} J ...

  8. ReplaceAll 特殊字符处理

    用到Json与replaceAll Http拦截脚本中经常需要替换,replace虽然不需要处理特殊字符,但是不能匹配多个,ReplaceAll能够使用正则,不过需要处理的转移实在太多 比如,需要替换 ...

  9. F Find the AFei Numbers

    链接:https://ac.nowcoder.com/acm/contest/338/F来源:牛客网 题目描述 AFei loves numbers. He defines the natural n ...

  10. jquery的扩展,及编辑插件的书写格式

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...