百度局域網異常訪問屏蔽策略破解方法


百度局域網異常訪問
百度屏蔽抓取頁面
php
抓取百度頁面
時間內向百度發出大量連接請求,即會受到百度局域網異常訪問屏蔽策略的限制。顯示出以下結果:很抱歉,您的電腦或所在的局域網絡有異常的訪問,此刻我們無法響應您的請求。 
請輸入以下驗證碼,即可恢復使用。
該頁面的出現嚴重影響了對百度產品頁面抓取的各種程序的功能。經研究,找到了2種較好的解決方法
1.PHP抓取頁面受限的破解方法
在使用PHP抓取百度知道問題頁時,由於PHP程序抓取速度過快,導致被屏蔽,采用以下的方式解決
抓取頁面需采用fsockopen方式,使用file_get_contents無法設置請求頭
fsockopen函數的使用方法請自行查閱,例子中的geturlcont函數為自定義函數,核心即為fsockopen,geturlcont函數的原型
geturlcont($url, $referer = "" ,$cookie),$url為待抓取的頁面的url,$referer為傳遞的referer參數(主要用於防止抓到的是百度首頁),$cookie為重要的一個參數,用於破解局域網異常訪問屏蔽功能
例子:
$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保存這個BAIDUVERIFY值,也可以用文件或數據庫來保存
}
其中$cookie的值將會作為請求頭中的cookie,當百度服務器接收到含有指定的正確的BAIDUVERIFY時就能正常的返回內容,但這個COOKIE的生存期應該在20分鍾內,當再次出現問題時,程序將重新自動修改cookie並繼續抓取頁面。
2.Chrome擴展直接跳轉方案
在另一個程序中,不需要抓取頁面內容,但會同時打開數十頁的百度頁面,同樣可能會造成短時間內請求過多導致的屏蔽,解決方法是通過Chrome擴展的js注入功能實現
例子:
//以下用到的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');//被屏蔽的時間的UNIX時間戳(服務器時間)
var di=baidu('input[name=di]').attr('value');//應該為與序列號對應的一個密碼,用於防止通過程序自行生成
baidu.cookie.setRaw('BAIDUVERIFY',vcode+':'+id+':'+di,{expires:600*1000,domain:'baidu.com'});//設置cookie,注意要設置在baidu.com域下,否則會設置到verify.baidu.com中
location.href=url;//跳回原頁面

附(百度屏蔽頁的代碼):
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>百度--您的訪問出錯了</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">您的訪問出錯了</div></div>
<div id="content">很抱歉,您的電腦或所在的局域網絡有異常的訪問,此刻我們無法響應您的請求。 <br>請輸入以下驗證碼,即可恢復使用。</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">&copy; 2012 Baidu <a href="http://www.baidu.com/duty/index.html">免責聲明</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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM