python ip代理_使用python获取IP代理列表

本文提供了一个简单的Python脚本示例,用于从activeproxy.net抓取并解析IP代理数据。脚本使用urllib2进行HTTP请求,SGMLParser进行HTML解析,最终将抓取的代理信息(包括国家、IP、匿名状态、速度和端口等)存储到文件中。
摘要由CSDN通过智能技术生成

无聊写点小例子玩玩,非常简单。

抓取的网站地址为:http://activeproxy.net/en/

主要就是urlopen获取内容,使用SGMLParser筛选代理数据,存储。

最后的存储文件片断:

checktime country proxyip anonymous speed port connect

02:57 Germany 94.249.168.231 no 1181 3128 0,006

03:14 Germany 93.180.156.21 no 807 3128 0,009

18:05 Germany 188.138.115.15 no 744 3128 0,009

06:31 Germany 91.250.83.172 no 578 3128 0,011

00:30 United States 5.153.4.90 no 606 3128 0,013

14:25 France 91.121.58.235 no 490 3128 0,014

抓取用的python脚本:

#!/usr/bin/python

import os

import urllib2

from sgmllib import SGMLParser

url = 'http://activeproxy.net/en/%d/'

class ProxyParser(SGMLParser):

def __init__(self):

SGMLParser.__init__(self)

self.proxies = []

self.update()

def update(self):

self.tr = False

self.td = False

self.proxy = {}

self.cur_attr = ''

self.count = 0

def start_tr(self, attr):

self.tr = True

def end_tr(self):

if (self.proxy):

self.proxies.append(self.proxy)

self.count += 1

self.tr = False

self.proxy = {}

def start_td(self, attr):

self.td = True

if self.tr:

self.cur_attr = attr[0][1][4:]

def end_td(self):

self.td = False

self.cur_attr = ''

def handle_data(self, text):

if self.tr and self.td and not text.startswith('\n'):

self.proxy[self.cur_attr] = text

i = 1

proxyParser = ProxyParser()

while True:

proxyParser.update()

print 'proceeding...', url % (i)

content = urllib2.urlopen(url % (i)).read()

content = content.decode('gb2312').encode('utf-8')

proxyParser.feed(content)

print proxyParser.count, 'fetched.'

i += 1

if i > 100 or proxyParser.count != 20:

break;

f = open('active_proxy', 'w')

str_format = '%10s%25s%20s%15s%10s%10s%10s' + os.linesep

if proxyParser.proxies:

f.write(str_format % tuple(proxyParser.proxies[0].keys()))

for item in proxyParser.proxies:

if len(item.values()) == 7:

f.write(str_format % tuple(item.values()))

f.close()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值