国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

PHP代碼實現(xiàn)爬蟲記錄——超管用

2024-05-04 23:38:30
字體:
來源:轉載
供稿:網友

這篇文章主要通過創(chuàng)建crawler數據庫,使用robot.php記錄來訪的爬蟲信息從而將信息插入數據庫,從而使用php代碼實現(xiàn)爬蟲記錄,有需要的小伙可以來參考下。

實現(xiàn)爬蟲記錄本文從創(chuàng)建crawler 數據庫,robot.php記錄來訪的爬蟲從而將信息插入數據庫crawler,然后從數據庫中就可以獲得所有的爬蟲信息。實現(xiàn)代碼具體如下:

數據庫設計

 

 
  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. $Url = $Url.":".$ServerPort ; 
  10. }  
  11. $Url=$Url.$ScriptName; 
  12. if ($QueryString !=""
  13. $Url=$Url."?".$QueryString; 
  14. }  
  15. $GetLocationURL=$Url ; 
  16. $agent = $_SERVER["HTTP_USER_AGENT"];  
  17. $agent=strtolower($agent); 
  18. $Bot =""
  19. if (strpos($agent,"bot")>-) 
  20. $Bot = "Other Crawler"
  21. if (strpos($agent,"googlebot")>-) 
  22. $Bot = "Google"
  23. }  
  24. if (strpos($agent,"mediapartners-google")>-) 
  25. $Bot = "Google Adsense"
  26. if (strpos($agent,"baiduspider")>-) 
  27. $Bot = "Baidu"
  28. if (strpos($agent,"sogou spider")>-) 
  29. $Bot = "Sogou"
  30. if (strpos($agent,"yahoo")>-) 
  31. $Bot = "Yahoo!"
  32. if (strpos($agent,"msn")>-) 
  33. $Bot = "MSN"
  34. if (strpos($agent,"ia_archiver")>-) 
  35. $Bot = "Alexa"
  36. if (strpos($agent,"iaarchiver")>-) 
  37. $Bot = "Alexa"
  38. if (strpos($agent,"sohu")>-) 
  39. $Bot = "Sohu"
  40. if (strpos($agent,"sqworm")>-) 
  41. $Bot = "AOL"
  42. if (strpos($agent,"yodaoBot")>-) 
  43. $Bot = "Yodao"
  44. if (strpos($agent,"iaskspider")>-) 
  45. $Bot = "Iask"
  46. require("./dbinfo.php"); 
  47. date_default_timezone_set('PRC');  
  48. $shijian=date("Y-m-d h:i:s", time()); 
  49. // 連接到 MySQL 服務器 
  50. $connection = mysql_connect ($host, $username, $password); 
  51. if (!$connection) 
  52. die('Not connected : ' . mysql_error()); 
  53. // 設置活動的 MySQL 數據庫 
  54. $db_selected = mysql_select_db($database, $connection); 
  55. if (!$db_selected) 
  56. die ('Can/'t use db : ' . mysql_error()); 
  57. // 向數據庫插入數據 
  58. $query = "insert into crawler (crawler_category, crawler_date, crawler_url, crawler_IP) values ('$Bot','$shijian','$GetLocationURL','$serverip')"
  59. $result = mysql_query($query); 
  60. if (!$result) 
  61. die('Invalid query: ' . mysql_error()); 
  62. ?> 

成功了,現(xiàn)在訪問數據庫即可得知什么時候哪里的蜘蛛爬過你的什么頁面。

 

 
  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. </table> 
  36. <?php 
  37. echo $pages -> myde_write(); 
  38. ?> 

以上代碼就是PHP代碼實現(xiàn)爬蟲記錄——超管用的全部內容,希望對大家有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 阳泉市| 蕉岭县| 年辖:市辖区| 上林县| 金湖县| 双辽市| 聊城市| 永济市| 南投县| 濉溪县| 云霄县| 兴安县| 铅山县| 个旧市| 河北区| 延吉市| 保德县| 木里| 资中县| 吉木乃县| 高邮市| 崇文区| 揭西县| 满城县| 炉霍县| 靖州| 武威市| 公安县| 湘潭市| 泽普县| 萨嘎县| 伊吾县| 木里| 白水县| 商南县| 罗田县| 平利县| 同心县| 绿春县| 富顺县| 克拉玛依市|