自动收集有效IP代理

自动收集有效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

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

猜你喜欢

转载自www.cnblogs.com/ly570/p/11211009.html