项目中需要用到经常采集搜索引擎的数据,而百度,google等对短时间内大流量搜索做了限制,会限制IP(百度) 等,google的更是做了识别本机生成cookie(猜测) 的限制,模拟cookie都不行。
大多数采集器都做了自动重启IP的功能, 当采集页面匹配到做了限制的 正则时,执行重启IP:
//$this->noData 为匹配限制的页面正则, $this->page 为页面内容,可用file_get_content()获取
if(preg_match($this->noData,$this->page)){
$this->reStartRouter();
}
//操作路由
function router($target=''")
{
$server = '192.168.0.1'; // IP address
$host = '192.168.0.1'; // Domain name
$port = 80;
$referer = 'http://'.$host.$target; // Referer
$username = "admin";#路由的用户名
$password = "admin";#路由的管理密码
$authorization = base64_encode($username.":".$password);
$File = fsockopen($server, $port, $errno, $errstr, 50);
if ($File)
{
$out = "GET $target HTTP/1.1\r\n";
$out .= "Host: $host\r\n";
$out .= "Referer: $referer\r\n";
$out .= "Authorization: Basic $authorization\r\n";
$out .= "Connection: Close\r\n\r\n";
fputs($File, $out);
$makeFile = $buffer = "";
while ($buffer = fread($File,4096))
{
$makeFile = $makeFile.$buffer;
}
fclose($File);
return $makeFile;
}
}
//断开
function disconnectr() {
$url = '/if.cgi?redirect=sys_status.htm&failure=fail.htm&
type=wan_state_reset&ifname=ppp0&ifstatus=Up&ifcmd=DISCONNECT';
router($url);
}
//连接
function connectr() {
$url = '/if.cgi?redirect=sys_status.htm&failure=fail.htm&
type=wan_state_reset&ifname=ppp0&ifstatus=Down&ifcmd=CONNECT';
router($url);
}
function reStartRouter()
{
$r=new router();
$r->disconnectr();//断开
$r->connectr();//连接
sleep(3);//静止3秒,一般要过一会才可以 连上
return true;
}