python 爬虫 批量获取代理ip的实例代码
实例如下所示:
importurllib.request
importos,re,sys,time
try:
fromStringIOimportStringIO
exceptImportError:
fromioimportStringIO
loca=re.compile(r"""ion":"\D+","ti""")
#伪装成浏览器
header={'User-Agent':'Mozilla/5.0(WindowsNT10.0;WOW64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/44.0.2403.157Safari/537.36'}
classGetip():
def__init__(self,diqu):
self.ur={"xicidaili国内普通代理--1线":"http://www.xicidaili.com/nt/",
"ip84国内普通代理--2线":'http://www.ip84.com/dlpn-http/',
'xicidaili国内高匿名代理--1线':'http://www.xicidaili.com/nn/',
'ip84国内高匿名代理--2线':'http://www.ip84.com/dlgn-http/',
'xicidaili国外高匿名代理--1线':'http://www.xicidaili.com/wn/',
'ip84国外高匿名代理--2线':'http://www.ip84.com/gwgn-http/',
'xicidaili国外普通代理--1线':'http://www.xicidaili.com/wt/',
'haodailiip国内混合代理--3线':'http://www.haodailiip.com/guonei/',
'haodailiip国外混合代理--3线':'http://www.haodailiip.com/guoji/',
}
self.diqu=diqu
defurlopen(self,url):
globalheader
try:
req=urllib.request.Request(url,None,header)
res=urllib.request.urlopen(req)
returnres
except:
pass
defgetip(self,ren):
'''url="http://proxy.ipcn.org/proxylist.html"#代理IP页面
ip_proxy_re=re.compile(r"""\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d{1,}""")#直接匹配xxx.xxx.xxx.xxx:xxxx'''
url=self.ur[self.diqu]+str(ren)
ip_proxy_re=re.compile(r'(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s*\s*\s*(\d{1,})\s* \s*<[^\u4E00-\u9FA5]+>([\u4E00-\u9FA5]*\s*[\u4E00-\u9FA5]*\s*[\u4E00-\u9FA5]*)\s*<')
#################################通用正则匹配的格式是(IP,端口,地区)地区有可能包含换行和空格
try:
data=self.urlopen(url).read().decode('utf-8')
except:
returnNone
self.rel=[]
ip=ip_proxy_re.findall(data)
##########返回的IP就是正则匹配的结果(IP,端口,地区)地区有可能包含换行和空格
returnip
if__name__=='__main__':
g=Getip("xicidaili国内普通代理--1线")
importpprint
forxinrange(4):
ips=g.getip(1)
print('获取到ip地址一共:',len(ips))
pprint.pprint(ips)
以上这篇python爬虫批量获取代理ip的实例代码就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。