局域網(wǎng)攔截是怎么回事 如何防止局域網(wǎng)屏蔽 目前,當(dāng)某個(gè)IP段在短時(shí)間內(nèi)向百度發(fā)出大量連接請求,即會受到百度局域網(wǎng)異常訪問屏蔽策略的限制。顯示出以下結(jié)果:很抱歉,您的電腦或所在的局域網(wǎng)絡(luò)有異常的訪問,此刻我們無法響應(yīng)您的請求。
請輸入以下驗(yàn)證碼,即可恢復(fù)使用。
該頁面的出現(xiàn)嚴(yán)重影響了對百度產(chǎn)品頁面抓取的各種程序的功能。經(jīng)研究,找到了2種較好的解決方法
1.PHP抓取頁面受限的破解方法
在使用PHP抓取百度知道問題頁時(shí),由于PHP程序抓取速度過快,導(dǎo)致被屏蔽,采用以下的方式解決
抓取頁面需采用fsockopen方式,使用file_get_contents無法設(shè)置請求頭
fsockopen函數(shù)的使用方法請自行查閱,例子中的geturlcont函數(shù)為自定義函數(shù),核心即為fsockopen,geturlcont函數(shù)的原型
geturlcont($url, $referer = "" ,$cookie),$url為待抓取的頁面的url,$referer為傳遞的referer參數(shù)(主要用于防止抓到的是百度首頁),$cookie為重要的一個(gè)參數(shù),用于破解局域網(wǎng)異常訪問屏蔽功能
例子:
$page=geturlcont("http://zhidao.baidu.com/question/1.html", $referer = "http://www.baidu.com/search/ressafe.html?q=&ms=3&url=http://zhidao.baidu.com/question/1.html" ,$_COOKIE['BAIDUVERIFY']);
//$page獲取到的是http://zhidao.baidu.com/question/1.html頁的代碼或空(被百度屏蔽)
if($page==NULL){//頁面獲取失敗
$page=file_get_contents("http://zhidao.baidu.com/question/1.html?oldq=1");//為了得到屏蔽頁需要使用file_get_contents
preg_match('//i',$page,$vcode);
preg_match('/"[0-9A-F]*"/i',$vcode[0],$vcode);
$vcode=str_replace('"',"",$vcode[0]);
preg_match('//i',$page,$id);
preg_match('/"[0-9]*"/',$id[0],$id);
$id=str_replace('"',"",$id[0]);
preg_match('//i',$page,$di);
preg_match('/"[0-9a-f]*"/i',$di[0],$di);
$di=str_replace('"',"",$di[0]);
setcookie('BAIDUVERIFY',$vcode.':'.$id.':'.$di.';');//本例中使用cookie保存這個(gè)BAIDUVERIFY值,也可以用文件或數(shù)據(jù)庫來保存
}
其中$cookie的值將會作為請求頭中的cookie,當(dāng)百度服務(wù)器接收到含有指定的正確的BAIDUVERIFY時(shí)就能正常的返回內(nèi)容,但這個(gè)COOKIE的生存期應(yīng)該在20分鐘內(nèi),當(dāng)再次出現(xiàn)問題時(shí),程序?qū)⒅匦伦詣有薷腸ookie并繼續(xù)抓取頁面。
2.Chrome擴(kuò)展直接跳轉(zhuǎn)方案
在另一個(gè)程序中,不需要抓取頁面內(nèi)容,但會同時(shí)打開數(shù)十頁的百度頁面,同樣可能會造成短時(shí)間內(nèi)請求過多導(dǎo)致的屏蔽,解決方法是通過Chrome擴(kuò)展的js注入功能實(shí)現(xiàn)
例子:
//以下用到的Tangram方法可參見http://tangram.baidu.com/api#baidu()
var url=baidu('input[name=url]').attr('value');//獲取屏蔽前訪問的URL
var vcode=baidu('input[name=vcode]').attr('value');//序列號
var id=baidu('input[name=id]').attr('value');//被屏蔽的時(shí)間的UNIX時(shí)間戳(服務(wù)器時(shí)間)
var di=baidu('input[name=di]').attr('value');//應(yīng)該為與序列號對應(yīng)的一個(gè)密碼,用于防止通過程序自行生成
baidu.cookie.setRaw('BAIDUVERIFY',vcode+':'+id+':'+di,{expires:600*1000,domain:'baidu.com'});//設(shè)置cookie,注意要設(shè)置在baidu.com域下,否則會設(shè)置到verify.baidu.com中
location.href=url;//跳回原頁面
附(百度屏蔽頁的代碼):
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>百度--您的訪問出錯(cuò)了</title>
<style>
body{text-align:center;margin-top:3px}
#wrap{width:650px;text-align:left;margin:auto}
#logo{float:left;margin:0 3px 0 0}
#logo img{border:0}
#title{float:left;width:510px}
#intitle{margin:20px 0 0 0;background-color:#e5ecf9;width:100%;font-weight:bold;font-size:14px;padding:3px 0 4px 10px}
#content{clear:left;padding-top:60px;line-height:200%}
#vf{margin-top:10px}
#vf img{float:left;border:1px solid #000}
#kw{font:16px Verdana;height:1.78em;padding-top:2px}
#vf form{float:left;margin:12px 0 0 5px;padding:0}
#ft{text-align:center}
#ft,#ft a{color:#666;font-size:14px}
</style>
</head>
<body>
<div id="wrap">
<div id="logo"><a href="http://www.baidu.com"><img alt="到百度首頁" title="到百度首頁" src="http://www.baidu.com/img/logo-yy.gif" width="137" height="46"></a></div>
<div id="title"><div id="intitle">您的訪問出錯(cuò)了</div></div>
<div id="content">很抱歉,您的電腦或所在的局域網(wǎng)絡(luò)有異常的訪問,此刻我們無法響應(yīng)您的請求。 <br>請輸入以下驗(yàn)證碼,即可恢復(fù)使用。</div>
<div id="vf">
<img src="http://verify.baidu.com/cgi-bin/genimg?9D5771ED69F27050263223420F7043C211B95E2D3616825774FF64E3FEF61920E3089D79E454888755B8F3AFDF124C9C74A34C4DDFAFD27827B3512B437BE76043" width="120" height="40">
<form action="http://verify.baidu.com/verify">
<input type="hidden" name="url" value="http://zhidao.baidu.com/question/519848902?oldq=1&vf_tf=1">
<input type="hidden" name="vcode" value="9D5771ED69F27050263223420F7043C211B95E2D3616825774FF64E3FEF61920E3089D79E454888755B8F3AFDF124C9C74A34C4DDFAFD27827B3512B437BE76043">
<input type="hidden" name="id" value="1359359285">
<input type="hidden" name="di" value="530e060fe3c68f3d">
<input type="text" size="6" maxlength="10" name="verifycode" id="kw">
<input type="submit" value="提交">
</form>
</div>
<div style="clear:left;height:90px"></div>
<div id="ft">© 2012 Baidu <a href="http://www.baidu.com/duty/index.html">免責(zé)聲明</a></div>
</div>
<script>
(function(){
var rfr = window.document.location.href,
p = encodeURIComponent(rfr),
img = new Image(),
imgzd = new Image(),
re = ///vcode/?http:////(/S+)/.baidu/ig,r="";
img.src = "http://nsclick.baidu.com/v.gif?pid=201&pj=vcode&path="+p+"&t="+new Date().getTime();
r = re.exec(rfr);
if(r&&r[1]){imgzd.src = "http://"+r[1]+".baidu.com/v.gif?fr=vcode&url="+p+"&t="+new Date().getTime();}
})();
</script>
</body>
</html>
新聞熱點(diǎn)
疑難解答