当前位置:首页 > PHP教程 > php高级应用 > 列表

PHP代码实现爬虫记录——超管用

发布:smiling 来源: PHP粉丝网  添加日期:2021-06-15 10:04:16 浏览: 评论:0 

这篇文章主要通过创建crawler数据库,使用robot.php记录来访的爬虫信息从而将信息插入数据库,从而使用php代码实现爬虫记录,有需要的小伙可以来参考下。

实现爬虫记录本文从创建crawler 数据库,robot.php记录来访的爬虫从而将信息插入数据库crawler,然后从数据库中就可以获得所有的爬虫信息。实现代码具体如下:

数据库设计

  1. create table crawler   
  2. (   
  3.  crawler_ID bigint() unsigned not null auto_increment primary key
  4.  crawler_category varchar() not null
  5.  crawler_date datetime not null default '-- ::'
  6.  crawler_url varchar() not null
  7.  crawler_IP varchar() not null 
  8. )default charset=utf; 

以下文件 robot.php 记录来访的爬虫,并将信息写入数据库:

  1. <?php 
  2.  $ServerName = $_SERVER["SERVER_NAME"] ;  
  3.  $ServerPort = $_SERVER["SERVER_PORT"] ;  
  4.  $ScriptName = $_SERVER["SCRIPT_NAME"] ;  
  5.  $QueryString = $_SERVER["QUERY_STRING"];  
  6.  $serverip = $_SERVER["REMOTE_ADDR"] ;  
  7.  $Url="http://".$ServerName
  8.  if ($ServerPort != ""
  9.  { 
  10.   $Url = $Url.":".$ServerPort ; 
  11.  }  
  12.  $Url=$Url.$ScriptName
  13.  if ($QueryString !=""
  14.  { 
  15.   $Url=$Url."?".$QueryString
  16.  }   
  17.  $GetLocationURL=$Url ; 
  18.  $agent = $_SERVER["HTTP_USER_AGENT"];  
  19.  $agent=strtolower($agent); 
  20.  $Bot =""
  21.  if (strpos($agent,"bot")>-) 
  22.  { 
  23.   $Bot = "Other Crawler"
  24.  } 
  25.  if (strpos($agent,"googlebot")>-) 
  26.  { 
  27.   $Bot = "Google"
  28.  }    
  29.  if (strpos($agent,"mediapartners-google")>-) 
  30.  { 
  31.   $Bot = "Google Adsense"
  32.  } 
  33.  if (strpos($agent,"baiduspider")>-) 
  34.  { 
  35.   $Bot = "Baidu"
  36.  } 
  37.  if (strpos($agent,"sogou spider")>-) 
  38.  { 
  39.   $Bot = "Sogou"
  40.  } 
  41.  if (strpos($agent,"yahoo")>-) 
  42.  { 
  43.   $Bot = "Yahoo!"
  44.  } 
  45.  if (strpos($agent,"msn")>-) 
  46.  { 
  47.   $Bot = "MSN"
  48.  } 
  49.  if (strpos($agent,"ia_archiver")>-) 
  50.  { 
  51.   $Bot = "Alexa"
  52.  } 
  53.  if (strpos($agent,"iaarchiver")>-) 
  54.  { 
  55.   $Bot = "Alexa"
  56.  } 
  57.  if (strpos($agent,"sohu")>-) 
  58.  { 
  59.   $Bot = "Sohu"
  60.  } 
  61.  if (strpos($agent,"sqworm")>-) 
  62.  { 
  63.   $Bot = "AOL"
  64.  } 
  65.  if (strpos($agent,"yodaoBot")>-) 
  66.  { 
  67.   $Bot = "Yodao"
  68.  } 
  69.  if (strpos($agent,"iaskspider")>-) 
  70.  { 
  71.   $Bot = "Iask"
  72.  } 
  73.  require("./dbinfo.php"); 
  74.  date_default_timezone_set('PRC');  
  75.  $shijian=date("Y-m-d h:i:s", time()); 
  76.  // 连接到 MySQL 服务器 
  77.  $connection = mysql_connect ($host$username$password); 
  78.  if (!$connection
  79.  { 
  80.   die('Not connected : ' . mysql_error()); 
  81.  } 
  82.  // 设置活动的 MySQL 数据库 
  83.  $db_selected = mysql_select_db($database$connection); 
  84.  if (!$db_selected
  85.  { 
  86.   die ('Can\'t use db : ' . mysql_error()); 
  87.  } 
  88.  // 向数据库插入数据 
  89.  $query = "insert into crawler (crawler_category, crawler_date, crawler_url, crawler_IP) values ('$Bot','$shijian','$GetLocationURL','$serverip')"
  90.  $result = mysql_query($query); 
  91.  if (!$result
  92.  { 
  93.   die('Invalid query: ' . mysql_error()); 
  94.  } 
  95. ?> 

成功了,现在访问数据库即可得知什么时候哪里的蜘蛛爬过你的什么页面。

  1. view sourceprint? 
  2. <?php 
  3. include './robot.php'
  4. include '../library/page.Class.php'
  5. $page = $_GET['page']; 
  6. include '../library/conn_new.php'
  7. $count = $mysql -> num_rows($mysql -> query("select * from crawler")); 
  8. $pages = new PageClass($count,,$_GET['page'],$_SERVER['PHP_SELF'].'?page={page}'); 
  9. $sql = "select * from crawler order by "
  10. $sql .= "crawler_date desc limit ".$pages -> page_limit.",".$pages -> myde_size; 
  11. $result = $mysql -> query($sql); 
  12. ?> 
  13. <table width=""
  14.  <thead> 
  15.   <tr>  
  16.    <td bgcolor="#CCFFFF"></td>  
  17.    <td bgcolor="#CCFFFF" align="center" style="color:#">爬虫访问时间</td>  
  18.    <td bgcolor="#CCFFFF" align="center" style="color:#">爬虫分类</td> 
  19.    <td bgcolor="#CCFFFF" align="center" style="color:#">爬虫IP</td> 
  20.    <td bgcolor="#CCFFFF" align="center" style="color:#">爬虫访问的URL</td> 
  21.   </tr> 
  22.  </thead> 
  23. <?php 
  24. while($myrow = $mysql -> fetch_array($result)){ 
  25. ?> 
  26. <tr> 
  27.  <td width=""><img src="../images/topicnew.gif" /></td> 
  28.  <td width="" style="font-family:Georgia"><? echo $myrow["crawler_date"] ?></td> 
  29.  <td width="" style="color:#FA"><? echo $myrow["crawler_category"] ?></td> 
  30.  <td width=""><? echo $myrow["crawler_IP"] ?></td> 
  31.  <td width=""><? echo $myrow["crawler_url"] ?></td> 
  32. </tr> 
  33. <?php 
  34.  } 
  35. ?> 
  36.  </table> 
  37. <?php 
  38.  echo $pages -> myde_write(); 
  39. ?> 

以上代码就是PHP代码实现爬虫记录——超管用的全部内容,希望对大家有所帮助。

Tags: PHP爬虫记录

分享到: