标签:
最近练习写爬虫,本来爬几张mm图做测试,可是爬到几十张的时候就会返回403错误,这是被网站服务器发现了,把我给屏蔽了。
因此需要使用代理IP。为了方便以后使用,我打算先写一个自动爬取ip代理的爬虫,正所谓,磨刀不误砍柴工,读完高中再打工!
先看看运行结果:
函数返回一个列表
废话少说,放码出去:
#-*- coding: utf-8 -*- import urllib import urllib2 import re import time # obtain some ip and port for spider from a site,xicidaili.com. class ObtainProxy: def __init__(self,region = ‘国内普通‘): self.region = {‘国内普通‘:‘nt/‘,‘国内高匿‘:‘nn/‘,‘国外普通‘:‘wt/‘,‘国外高匿‘:‘wn/‘,‘SOCKS‘:‘qq/‘} self.url = ‘http://www.xicidaili.com/‘ + self.region[region] self.header = {} self.header[‘User-Agent‘] = ‘Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36‘ def get_prpxy(self): req = urllib2.Request(self.url,headers = self.header) resp = urllib2.urlopen(req) content = resp.read() self.get_ip = re.findall(r‘(\d+\.\d+\.\d+\.\d+)</td>\s*<td>(\d+)</td>‘,content) self.pro_list = [] for each in self.get_ip: a_info = each[0] + ‘:‘ + each[1] self.pro_list.append(a_info) return self.pro_list def save_pro_info(self): with open(‘proxy‘,‘w‘) as f: for each in self.get_ip: a_info = each[0] + ‘:‘ + each[1] + ‘\n‘ f.writelines(a_info) if __name__ == ‘__main__‘: proxy = ObtainProxy() print proxy.get_prpxy()
这个玩意还是挺好使的。
标签:
原文地址:http://www.cnblogs.com/zhangte/p/5184215.html