(编辑:jimmy 日期: 2024/11/16 浏览:2)
本文实例讲述了python实现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()
更多关于Python相关内容可查看本站专题:《Python Socket编程技巧总结》、《Python正则表达式用法总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》
希望本文所述对大家Python程序设计有所帮助。