第一时间获取 Python 及自动化技术干货
/1 前言/
玩爬虫的都避免不了各大网站的反爬措施限制,比较常见的是通过固定时间检测某 ip 地址访问量来判断该用户是否为 “网络机器人”,也就是所谓的爬虫,如果被识别到,就面临被封 ip 的风险,那样你就不能访问该网址了。
通用的解决办法是用代理 ip 进行爬取,但是收费的代理 ip 一般都是比较贵的,网上倒是有很多免费的代理ip网站,但是受时效性影响,大部分地址都不能用,有很多维护代理 ip 池的教程,即把爬取并检测后能用代理 ip 放到“代理池里”,等以后要用的时候再从里面提取,在我看来,这种效率比较低,因为这类 ip 地址很快就失效,我们要做的是边检测边使用,充分保证免费 ip 的时效性。
/2 抓取 IP 地址/
下面就开始实战操作。
1.首先我们随便找一个免费代理 ip 网站,如下图所示。
2、打开网页查看器,分析其网页元素结构,如下图所示。
3、就是一个简单的静态网页,我们用 requests 和 bs4 将 ip 地址和对应端口爬下,如下图所示。
4、每一行 ip 地址都由 5 个
标签组成,而我们需要的是第一个标签(对应 IP 地址)和第 2 个标签(对应端口),所以从第一个开始,每隔 5 个取出 ip 地址(item[::5]),从第二个开始,每隔 5 个取出对应端口(item[1::5]),参数 n 为页码,每次只在 1 页取 1 个有用的 ip 地址,最终效果如下图所示:
/3 验证 IP 有效性/
这里把百度百科作为目标网站,这个看似很普通的网站,反爬措施却极为严格,爬不了几条内容就开始请求失败了,下面我以在百度百科查询全国火车站归属地信息为例演示如何使用免费代理 ip。
1、首先我在 12306 上把所有的火车站名都爬下来了,但是没有归属地信息。
2、然后以站名构造百度百科 url 信息,分析网页元素,把爬取爬取火车站地址信息,网页元素如下图所示:
3、所以,我们只需在 class_='basicInfo-item' 的标签内容里查找有无“省”或者“市”的字符,然后输出就行了,最后加一个 while True 循环,当该 ip 能正常爬数据时,则 break 该循环;
若该 ip 被禁,则马上重新请求一个新ip进行爬取。
直接上代码如下图所示:
4、其中 for 循环是遍历所有火车站,try 是用于检测该 ip 还能不能用,若不能,则在 except 里请求 1 个新 ip,爬取效果如下图所示:
下次再遇到爬虫被禁的情况就可以用此办法解决了。
/4 结语/
本文基于 Python 网络爬虫技术,主要介绍了去 IP 代理网站上抓取可用 IP,并且 Python 脚本实现验证 IP 地址的时效性,如遇到爬虫被禁的情况就可以用本文的办法进行解决。
文中所有的代码,我已上传到后台,关注公众号后回复
代理
获取完整的代码。
每日
留言
说说你最近遇到的有趣事情
?
或者一句激励自己的话?
(字数不少于15字)
留
言
赠
书
《机器学习入门》
微信扫一扫
关注该公众号
![]()
微信扫一扫
使用小程序
',
config:[{
querySelector:"redpacketcover",
genId:function(){
var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];
return decodeURIComponent(e.node.getAttribute("data-coveruri")||"");
},
calW:function(){
var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];
return.7854*e.parentWidth;
},
calH:function(){
var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];
return this.calW({
parentWidth:e.parentWidth
})/.73346+27+37;
},
replaceContentCssText:"",
appendContentCssText:"display: inline-block;position: relative;",
outerContainerLeft:'
"
},{
querySelector:"qqmusic",
genId:function(){
var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];
return(e.node.getAttribute("musicid")||"").replace(/^\s/,"").replace(/\s$/,"")+"_"+e.index;
},
calW:function(){
var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];
return 1*e.parentWidth;
},
calH:function(){
return 88;
},
replaceContentCssText:"",
appendContentCssText:"margin:16px 0;diplay:block;",
outerContainerLeft:"",
outerContainerRight:""
},{
querySelector:"mpvoice",
genId:function(){
var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],t=decodeURIComponent(e.node.getAttribute("voice_encode_fileid")||"").replace(/^\s/,"").replace(/\s$/,"");
return t+"_"+e.index;
},
calW:function(){
var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];
return 1*e.parentWidth;
},
calH:function(){
return 122;
},
replaceContentCssText:"",
appendContentCssText:"margin:16px 0;diplay:block;",
outerContainerLeft:"",
outerContainerRight:""
}]
};
e(t);
}(function(){
var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];
if("function"==typeof document.querySelectorAll)for(var t={
maxWith:document.getElementById("img-content").getBoundingClientRect().width,
idAttr:"data-preloadingid"
},n=0,r=e.config.length;r>n;n++)for(var i=e.config[n],a=document.querySelectorAll(i.querySelector),o=0,g=a.length;g>o;o++){
var s=a[o],l=s.parentNode.getBoundingClientRect().width;
l=Math.min(l,t.maxWith);
var d=i.calW({
parentWidth:l
}),c=i.calH({
parentWidth:l
}),u=i.genId({
index:o,
node:s
}),p=e.defaultContentTpl.replace(/#height#/g,c).replace(/#width#/g,d),h=document.createElement("div");
if(h.innerHTML=p,i.replaceContentCssText){
var m=i.replaceContentCssText.replace(/#height#/g,c).replace(/#width#/g,d);
h.firstChild.style.cssText=m;
}else i.appendContentCssText&&(h.firstChild.style.cssText+=i.appendContentCssText);
var A=i.outerContainerLeft+h.innerHTML+i.outerContainerRight;
h.innerHTML=A,h.firstChild.setAttribute(t.idAttr,u),s.parentNode.insertBefore(h.firstChild,s.nextSibling);
}
});
前往“发现”-“看一看”浏览“朋友在看”