爬取的代理源为西刺代理。
- 用xpath解析页面
- 用telnet来验证ip是否可用
- 把有效的ip写入到本地txt中。当然也可以写入到redis、mongodb中,也可以设置检测程序当代理池中的ip数不够(如:小于20个)时,启动该脚本来重新获取ip,本脚本的代码也要做相应的改变。
# !/usr/bin/env python
# -*- coding: utf-8 -*-
# @Version : 1.0
# @Time : 2018/10/23 上午10:40
# @Author : Yeoman
# @Description :
import urllib.request
import lxml.etree
import telnetlib
import os
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.67 Safari/537.36'
}
def get_proxy(page_num):
# 获取页面
req = urllib.request.Request('http://www.xicidaili.com/nn/{}'.format(page_num), headers=headers) # 构造request请求
response = urllib.request.urlopen(req) # 发送请求
html = response.read()
html = html.decode('utf-8')
# print(html)
# 解析页面
proxy_list = []
selector = lxml.etree.HTML(html)
rows = selector.xpath('//*[@id="ip_list"]//tr')
rows_total = len(rows)
row_xpath_head = '//*[@id="ip_list"]//tr['
row_ip_xpath_tail = ']/td[2]/text()'
row_port_xpath_tail = ']/td[3]/text()'
for i in range(1, rows_total):
ip_xpath = row_xpath_head + str(i+1) + row_ip_xpath_tail
port_xpath = row_xpath_head + str(i+1) + row_port_xpath_tail
ip = selector.xpath(ip_xpath)[0]
port = selector.xpath(port_xpath)[0]
ip_port = ip + ':' + port
proxy_list.append(ip_port)
return proxy_list
# 检测代理ip是否可用
def test_proxy_ip_port(proxy_ip_port):
print('当前代理ip:{}'.format(proxy_ip_port))
ip_port = proxy_ip_port.split(':')
ip = ip_port[0]
port = ip_port[1]
# 用telnet来验证ip是否可用
try:
telnetlib.Telnet(ip, port, timeout=10)
except:
return False
else:
return True
# 把有效的ip写入本地
def write_ip(proxy_ip):
with open('./ip.txt', 'a') as f:
f.write(proxy_ip + '\n')
# 删除文件
def del_file():
file_path = './ip.txt'
if os.path.exists(file_path):
os.remove(file_path)
def run():
del_file()
proxy_ip_port_list = []
for i in range(1, 6): # 前5页
proxy_ip_port_list += get_proxy(i)
for i in range(100): # 一页有100条
proxy_ip_port = proxy_ip_port_list[i]
is_valid = test_proxy_ip_port(proxy_ip_port)
print(is_valid)
if is_valid:
# 写入ip到本地
write_ip(proxy_ip_port)
if __name__ == '__main__':
run()