此搜索引擎适于在一个中等规模的局域网中使用,由于找到的网页存在数据库中,不仅可以索静态的html页面,可以搜索php、asp等动态页面。对于一个拥有5万个网页的系统(使用pii-400作为服务器),搜索响应时间在2-10秒左右,完全可以满足要求,由于java、mysql、php都是跨平台的软件,所以此搜索引擎不仅可以工作在windows服务器上,而且也可以工作在linux等其他系统中。
一、建立搜索引擎需要的数据库和数据表。
首先建立数据库:
c:/mysql/bin/> mysqladmin -uroot -pmypasswd create spider
然后建立数据库中的表结构
c:/mysql/bin/> mysql -uroot -pmypasswd spider < spider.mysql
其中spider.mysql为一个文本文件,其内容如下:
| create table link ( id int(10) unsigned not null auto_increment, url varchar(120) not null, class tinyint(3) unsigned not null default 0 , issearchlink tinyint(3) unsigned default 0, primary key (url), unique id (id), key url (url), key class (class) ); |
# 本局域网的初始主页地址,搜索蜘蛛从此网址开始搜索所有其他网页
| insert into link values( '1', 'http://102.211.69.1/', '0', '0'); |
# 数据表 webpagelocal 用来存放下载的所有的网页
| create table webpagelocal ( id int(10) unsigned not null auto_increment, url varchar(120) not null, content text not null, primary key (url), unique id (id), key url (url) ); |
# 数据表 webpagefindfast
# 用makefast.php从表webpagelocal中提取512字节的检索信息存放其中
| create table webpagefindfast ( id int(10) unsigned not null, url varchar(120) not null, title varchar(64), content blob, primary key (url), key url (url), key title (title) ); |
二、以下为搜索网页和下载网页至本地数据库的java程序linktodb.java,它也是此搜索引擎的核心和基础
/***************************** linktodb.java *********************************** class counter { public class linktodb { linktodb(string myurl,string startonly){ collection values() { return counts.values(); } public static void main(string[] argv) throws filenotfoundexception { |
三、编译和运行此java程序
| d:/spider/> set classpath=d:/j/mm.mysql.jdbc2; d:/spider/> d:/j/bin/javac linktodb.java d:/spider/> d:/j/bin/java linktodb |
其中第一行命令是设置mysql的jdbc驱动程序路径。
四、由于网页中含有大量的无用的格式信息,直接用它来搜索要浪费大量的时间,所以需要掉其中的html格式控制信息,并将太长的网页截短,然后将整理后的用于搜索的信息存到另一个数据表中。由于php4中有一个很方便的函数strip_tags可以去掉其中的html格式标记,所以我们用php来整理。
makefast.php的内容如下:
| <?php mysql_connect("localhost","root","mypassword"); $result = mysql_db_query("spider","select id,url,content from webpagelocal whe re id>$n1 and id<$n2"); while($mt = mysql_fetch_array($result)){ $title = ""; $body = ""; $mt2 = strtoupper($mt[2]); $postitlel = strpos($mt2,"<title>"); $postitler = strpos($mt2,"</title>"); $posbody = strpos($mt2,"<body"); $posheadr = strpos($mt2,"</head>"); if($postitlel&&$postitler) $title = substr($mt[2],$postitlel+7,$postitl er-$postitlel-7); $title = eregi_replace("'","’",$title); if($posbody) $body = substr($mt[2],$posbody); else if($posheadr) $body = substr($mt[2],$posheadr+7); else if($postitler) $body = substr($mt[2],$postitler+8); else if($postitlel) $body = substr($mt[2],$postitlel); else $body = $mt[2]; $bodytext = strip_tags($body); $bodynospace = eregi_replace(" ","",$bodytext); $bodynoquote = eregi_replace("'","",$bodynospace); $body512 = substr($bodynoquote,0,511)." "; $id = $mt[0]; $url = $mt[1]; $sql="insert into webpagefindfast(id,url,title,content) values($id,'$url','$title','$body512')"; mysql_db_query("spider",$sql) or die($sql); echo $id." "; } ?> |
使用方式:
在浏览器中输入http://mywebsite/spider/makefast.php?n1=1&n2=10000
五、以上是建立搜索引擎所用到的数据,下面编制用于用户搜索的网页和php脚本文件。首先是用于搜索的表单页面searchform.htm,其内容如下。
<html> <body bgcolor="#eeffee" text="#000000"> |
search.php根据用户输入的条件,完成搜索,显示找到的网页的链接地址、标题和提要信息
。内容如下:
| <title>红蜘蛛正在搜索关键词为[<?php echo $keywords?>]的网页</title> <link rel="stylesheet" href="../all.css" type="text/css"> <body bgcolor="#eeffee"> <table width="96%" border="0" cellspacing="2" cellpadding="2" align="center" bgc olor="#99cc00"> <tr> <td> <div align="center"><font color="#ff0000"><b><span class="pt16">红蜘蛛搜索 引擎</span> <span class="pt12">v0.1</span></b></font></div> </td> </tr> </table> <form name="form1" method="post" action="search.php"> <table width="96%" border="1" cellspacing="1" cellpadding="1" align="center" b ordercolor="#99cc00"> <tr> <td valign="top"> <font color="#ff0000"><b><span class="pt13">关键字</span ></b><span class="pt13">:</span></font> <input type="text" name="keywords" value="<?php echo $keywords?>" size=" 30" maxlength="30"> <input type="submit" name="submit" value="重新搜索"> </td> <td valign="top"><font color="#ff0000"><b><span class="pt13">查找范围</spa n></b><span class="pt13">:</span></font> <select name="searchin"> <option value="content" <?php if ($searchin=="content") echo "selected ";?>>网页正文</option> <option value="title" <?php if ($searchin=="title") echo "selected ";?>>网页标题</option> </select> </td> <td valign="top"> <div align="left"><font color="#ff0000"><b><span class="pt13">查找方式</ span></b><span class="pt13">:</span></font> <select name="speed"> <option value="fast" <?php if ($speed=="fast") echo "selected";?>>快 速查找</option> <option value="slow" <?php if ($speed=="slow") echo "selected";?>>更 深查找</option> </select> </div> </td> </tr> </table> </form> <?php if($searchin=="title") $sql="select id,url,title,content from webpagefindfast where "; else $sql="select id,url,title,content from webpagefind$speed where "; $keywords=str_replace(" ", " ", $keywords); if($keywords=="") {echo "关键字不能为空"; exit();} $tok = strtok($keywords," "); $i=0; $j=0; while($tok) { $i++; $tok = strtok(" "); } $key = strtok($keywords," "); while($key) { $j++; if(substr($key,0,1)!="-") { $sql=$sql.$searchin." like '%".$key."%' "; $words[]=$key; } else { $sql=$sql.$searchin." not like '%".substr($key,1)."%' "; } if($j<$i) $sql.=" and "; $key = strtok(" "); } if($curpos!="") $sql.=" and id>=$curpos "; $sql.=" limit 100"; //echo "/$sql=".$sql." "; mysql_connect("localhost","root","mypassword"); $result=mysql_db_query("spider",$sql); $rowcount=mysql_num_rows($result); $findcount=0; ?> <table border=0 align=center width="96%"> <tr> <th nowrap width="41%"> <div align="left" class="pt12">共找到关键字为 <font color=red> <?php echo $keywords?> </font> 的网页共 <font color=red> <?php echo $rowcount;?> </font> 个</div> </th> <td nowrap> </td> </tr> <tr bgcolor="#ff0000"> <th nowrap colspan="2" height="3"></th> </tr> <?php while($row= mysql_fetch_array($result)){ $pos=$row[0]; $findcount++; if($findcount>20) break;?> <tr> <td nowrap colspan="2"> <?php echo $findcount;?> <a href="<?php echo $row[1]?>" target=_black> <?php if($row[2]!="") echo $row[2]; else echo substr($row[3],0,64); ?> </a></td> </tr> <tr> <td colspan="2" ><span class="pt13">摘要:</span> <?php if($searchin=="title") { $zhaiyao=substr($row[3],0,1024); } else{ if($speed=="fast") $zhaiyao=$row[3]; else{ $rowlen=strlen($row[3]); if ($rowlen<1024) { $zhaiyao=$row[3]; } else { $cutpos=0; $posword1=strpos($row[3],$words[0]); if($posword1-512<0) $zhaiyao=substr($row[3],0,1024); else { for($i=24;$i<500;$i++){ //避免将中文字符从半个字处截断,选择从英文处截断 if(ord(substr($row[3],$posword1-$i,1))<128) {$cutpos=$i;break;} } $zhaiyao=substr($row[3],$posword1-$cutpos,1024); } } } for($i=0;$i<count($words);$i++){ $zhaiyao=str_replace($words[$i],"<font color=red>".$words[$i]."</font>", $zhaiy ao); } } echo $zhaiyao; ?> </td> </tr> <tr> <td colspan="2" align="right"><a href="one.php?num=<?php echo $row[0]?>" tar get=_black> <font color="#0033ff" class="pt12">本地镜像</font></a></td> </tr> <tr bgcolor="#999933"> <td nowrap colspan="2" height="1"></td> </tr> <?php } ?> <?php if($rowcount>20){ ?> <tr> <td align="right" colspan="2" height="10"> <form name="form2" method="post" action="search.php"> <input type="hidden" name="keywords" value="<?php echo $keywords;?>"> <input type="hidden" name="searchin" value="<?php echo $searchin;?>"> <input type="hidden" name="speed" value="<?php echo $speed;?>"> <input type="hidden" name="curpos" value="<?php echo $pos;?>"> <input type="submit" name="submit" value="下20个网页"> </form> </td> </tr> <?php } ?> </table> |
one.php用于从本地镜像中显示一个找到的网页。由于网页的原始信息已经在webpagelocal中存储,所以
只需简单的读出,发给用户。
<?php |
闽公网安备 35060202000074号