上次讲了如何抓取代理IP,以及怎么将网页中的代理IP提取出来并存储到数据表中,详情见上一篇文章:[代码狗博客教程]使用PHP定时爬取代理IP。今天我们来讲怎么对mysql数据表中的代理IP进行验证,去掉无效的IP,只留下能用的。
首先,代理IP的有效性验证可以参考代码狗博客的文章:用PHP的curl方法验证代理IP的可用性,当然这种方法验证速度上相当的缓慢,快速的办法咱们以后再讨论,先实现功能。然后将数据表中的数据取出来进行验证,有效的就写上有效的标记,无效就写上无效的标记,当一次数据表内容验证完成之后,就对数据表中无效的代理IP进行删除即可!下面是具体代码。
文件名:protyipyz.php
复制
<?php
/**
*
*
* @version 1.0
* @copyright 2017
*/
ignore_user_abort(); // 后台运行
set_time_limit(0); // 取消脚本运行时间的超时上限
$interval=60*5;
// 每隔5分钟运行,这个间隔时间是可以随着 需要进行修改
do{
for($j=0;$j<getsl()+1;$j++){
timechaxun($j);
}
delectwx();
sleep($interval); // 休眠5分钟
}while(true);
//删除无效数据方法
function delectwx(){
//数据库地址
$servername = "localhost";
//数据库用户名
$username = "root";
//数据库密码
$password = "root";
// 创建连接
$con = mysql_connect($servername, $username, $password);
// 检测连接
if (!$con) {
die("数据库连接失败: " . mysql_error());
}else{
mysql_select_db("protyip",$con);
$sql="delete from protyip where yzzt='0'";
$fh=mysql_query($sql,$con);
}
mysql_close($con);
}
//获取数据表中所有数据的数量
function getsl(){
//数据库地址
$servername = "localhost";
//数据库用户名
$username = "root";
//数据库密码
$password = "root";
// 创建连接
$con = mysql_connect($servername, $username, $password);
// 检测连接
if (!$con) {
die("数据库连接失败: " . mysql_error());
}else{
mysql_select_db("protyip",$con);
$sql="select count(*) from protyip";
$fh=mysql_query($sql,$con);
$fhaa=mysql_fetch_array($fh);
echo var_dump($fhaa);
return $fhaa['count(*)'];
}
mysql_close($con);
}
//修改数据表中代理IP的状态
function timechaxun($n){
//数据库地址
$servername = "localhost";
//数据库用户名
$username = "root";
//数据库密码
$password = "root";
// 创建连接
$con = mysql_connect($servername, $username, $password);
// 检测连接
if (!$con) {
die("数据库连接失败: " . mysql_error());
}else{
mysql_select_db("protyip",$con);
$fh=mysql_query("select * from protyip limit ".$n.",1",$con);
// echo "select * from protyip limit "."0".",1";
while($row=mysql_fetch_array($fh)){
$xiaodata= $row['zqtime'];
$ipstr= $row['ip'];
//echo $xiaodata.$ipstr;
}
if(GetHttpStatusCode($ipstr)==200){
$datime=gettime();
//echo timecha($datime,$xiaodata);
$sql="UPDATE protyip set yztime='".$datime."',chtime='".timecha($datime,$xiaodata)."',yzzt='1' where ip='".$ipstr."'";
$fh=mysql_query($sql,$con);
}else{
$sql="UPDATE protyip set yztime='".$datime."',chtime='0000-00-00 00:00:00' where ip='".$ipstr."'";
$fh=mysql_query($sql,$con);
//echo $sql;
}
}
mysql_close($con);
}
//代理IP验证方法
function GetHttpStatusCode($proxy){
$curl = curl_init();
curl_setopt ($curl, CURLOPT_PROXY, $proxy);//使用代理访问
curl_setopt($curl,CURLOPT_URL,"http://www.baidu.com");//获取内容url
curl_setopt($curl,CURLOPT_HEADER,1);//获取http头信息
curl_setopt($curl,CURLOPT_NOBODY,1);//不返回html的body信息
curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);//返回数据流,不直接输出
curl_setopt($curl,CURLOPT_TIMEOUT,5); //超时时长,单位秒
curl_exec($curl);
$rtn= curl_getinfo($curl,CURLINFO_HTTP_CODE);
curl_close($curl);
return $rtn;
}
//计算代理IP存活时间差
function timecha($datime,$xiaotime){
$startdate=$xiaotime;
$enddate=$datime;
$date=floor((strtotime($enddate)-strtotime($startdate))/86400);
$hour=floor((strtotime($enddate)-strtotime($startdate))%86400/3600);
$minute=floor((strtotime($enddate)-strtotime($startdate))%86400/60-($hour*60));
$second=floor((strtotime($enddate)-strtotime($startdate))%86400%60);
$chatiome= $date."天".$hour."小时".$minute."分钟".$second."秒";
return $chatiome;
}
//获取服务器时间
function gettime(){
date_default_timezone_set("Asia/Hong_Kong");
$time=Date("Y-m-d H:i:s");
return $time;
}
?>效果如下图所示:
真是无语,一大片代理IP,验证后可用的就这几个……
注:前一篇文章中创建的数据表没有yzzt(验证状态)字段,如果需要请执行下面代码进行添加即可!
复制
alter table 数据表名 add 增加的字段名 字段类型 default '0'; //最后是默认数据为0
使用方法:例如在MyClass数据表中增加一个默认值为0,最大长度为4的整形数据的passtest字段。
alter table MyClass add passtest int(4) default ‘0’;







评论 (0)