2017-02-23
局域网拦截是怎么回事 如何防止局域网屏蔽 目前,当某个IP段在短时间内向百度发出大量连接请求,即会受到百度局域网异常访问屏蔽策略的限制。显示出以下结果:很抱歉,您的电脑或所在的局域网络有异常的访问,此刻我们无法响应您的请求。
请输入以下验证码,即可恢复使用。
该页面的出现严重影响了对百度产品页面抓取的各种程序的功能。经研究,找到了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">© 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>
像素拼图最新版下载v5.01 安卓版
其它手游 23.76MB
下载
机器人战斗竞技场手机版下载v3.71 安卓版
其它手游 77.19MB
下载
果冻人大乱斗最新版下载v1.1.0 安卓版
其它手游 113.98MB
下载
王者100刀最新版下载v1.2 安卓版
其它手游 72.21MB
下载
trueskate真实滑板正版下载v1.5.102 安卓版
其它手游 83.48MB
下载
矢量跑酷2最新版下载v1.2.1 安卓版
其它手游 119.39MB
下载
休闲解压合集下载v1.0.0 安卓版
其它手游 33.41MB
下载
指尖游戏大师最新版下载v4.0.0 安卓版
其它手游 74.77MB
下载飞天萌猫下载v3.0.3 安卓版
下载
火柴人越狱大逃脱下载v1.1 安卓版
下载
末日战场生死局下载v1.0 安卓版
下载
安静书女孩世界下载v1.0 安卓版
下载
消个寿司下载v1.00 免费版
下载
趣味找猫猫下载v1.1.2 安卓版
下载
方块男孩大战恶霸最新版下载v1.0.18 安卓版
下载
根本难不倒我下载v1.0.1 最新版
下载