0 Comments

获得需采集内容列表

发布于:2012-11-21  |   作者:广州网站建设  |   已聚集:人围观
获得需采集内容列表

前面介绍的爬虫程序,通过预先设置的规则将数据从目标站点下载到数据库中。现在需要写一个脚本程序将数据库中的内容以列表的形式展示出来,同时还要提供"修改"链接用来编辑、调整内容。采集内容列表程序的代码如下:

广州网站建设
  1. <?php 
  2. $con = mysql_connect ( 'localhost', 'root', 'password' ) or die ( 'Could not connect: ' . mysql_error () );  
  3. mysql_query ( "set names gb2312" );                 //设定数据字符集  
  4. echo 'Connected successfully';  
  5. $db = mysql_select_db ( 'get_content', $con );          //实例化数据库连接  
  6. if (! $db) {  
  7.     die ( "Can\'t use download : " . mysql_error () );  //数据库异常报错抛出  
  8. } else {  
  9.     //获得文件详细信息  
  10.     $sql = "SELECT * FROM 'articles' LIMIT 0,10 ";  
  11.     $result = mysql_query ( $sql, $con );  
  12.     $num_rows = mysql_num_rows ( $result );  
  13.  
  14.     //$rows=mysql_fetch_array($result);  
  15. //var_dump ($rows);  
  16. }  
  17.  
  18. ?> 
  19. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www. w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  20. <html xmlns="http://www.w3.org/1999/xhtml"> 
  21. <head> 
  22. <meta http-equiv="Content-Type" content="text/html; charset=gbk" /> 
  23. </head> 
  24. <link href="admin.css" rel="stylesheet" type="text/css" /> 
  25.  
  26. <base target="mainFrame"> 
  27. <div class="m"></div> 
  28. <div class="t"> 
  29. <table align=center cellspacing=0 cellpadding=0> 
  30.     <tr class=head> 
  31.         <td colspan="4">栏目名称</td> 
  32.     </tr> 
  33.     <tr class=line> 
  34.         <td><strong>日期</strong></td> 
  35.         <td width="913"><strong>标题</strong></td> 
  36.         <td><strong>修改</strong></td> 
  37.  
  38.     </tr> 
  39. <?php 
  40. for($i = 0; $i < $num_rows; $i ++) {  
  41. ?> 
  42. <tr class=line> 
  43.         <td width="135"> 
  44. <?php 
  45.     $row = mysql_fetch_array ( $result );  
  46.     echo $row ['Date'];  
  47. ?> 
  48. </td> 
  49.         <td>    
  50. <?php 
  51.     echo $row ['Title'];  
  52. ?> 
  53. </td> 
  54.         <td width="83"><a 
  55.             href="articles_save.php?aid=<? phpecho $row ['ID'];?>" target="_top">           修改</a></td> 
  56. </tr> 
  57. <?php 
  58. }  
  59. ?> 
  60. <tr class=line> 
  61.         <td colspan="4" align="center">&nbsp;</td> 
  62.     </tr> 
  63. </table> 
  64. </div> 

广州网站设计

将上述代码保存为articles.php文件。上述代码并不复杂,核心内容是将数据库中articles表中的内容取出,以列表形式输出到浏览器中。在浏览器中运行articles.php文件的效果如图8.4所示。
(点击查看大图)图8.4  文章列表
标签:
飞机