百度提供了分类搜索的API,在NET可以实现,效果图如下:
新建一个Defalut.aspx页面文件,在<body>..............</body>标签中插入如下代码:
<body>
<center>
<form name="search_form1" action="" οnsubmit="return gowhere(this)" target="_blank">
<table border="0" cellspacing="0" cellpadding="0" style="background-color: #FFFFFF;
width: 460; height: 60; font-family: 宋体;">
<tr>
<td>
<table border="0" cellspacing="0" cellpadding="0" style="width: 460; height: 80;
font-family: 宋体">
<tr>
<td>
<input type="hidden" name="myselectvalue" value="0" />
<input type="hidden" name="tn" />
<input type="hidden" name="ct" />
<input type="hidden" name="lm" />
<input type="hidden" name="cl" />
<input type="hidden" name="rn" />
</td>
</tr>
<tr>
<td style="width: 8%" valign="bottom">
<div align="center">
<a href="http://www.baidu.com/">
<img src="http://img.baidu.com/img/logo-80px.gif" style="border: 0" alt="Baidu" align="bottom" />
</a>
</div>
</td>
<td style="width: 92%;" valign="bottom">
<span style="padding: 0px">
<input type="radio" name="myselect" οnclick="javascript:this.form.myselectvalue.value=4;"
value="0" /><font color="#0000cc" style="font-size: 12px">新闻</font></span>
<input type="radio" name="myselect" οnclick="javascript:this.form.myselectvalue.value=0;"
value="0" checked="checked" />
<span><font color="#0000cc" style="font-size: 12px">网页</font></span>
<input type="radio" name="myselect" οnclick="javascript:this.form.myselectvalue.value=1;"
value="1" />
<span><font color="#0000cc" style="font-size: 12px">mp3</font></span>
<input type="radio" name="myselect" οnclick="javascript:this.form.myselectvalue.value=6;"
value="0" />
<span><font color="#0000cc" style="font-size: 12px">贴吧</font></span>
<input type="radio" name="myselect" οnclick="javascript:this.form.myselectvalue.value=5;"
value="0" />
<span><font color="#0000cc" style="font-size: 12px">图片</font></span>
<table align="right" border="0" cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td>
<font style="font-size: 12px">
<input id="word" name="word" size="35" /></font>
<input type="submit" value="百度搜索" />
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</center>
</body>
在用js实现分类的搜索功能,再在Default.aspx页面的<head>.............</head>之间插入js代码,如下:
<head runat="server">
<title>BaiduSearch</title>
<script type="text/javascript">
function gowhere(formname)
{
var url;
if(formname.myselectvalue.value=="0")
{
url="http://www.baidu.com/baidu";
document.search_form1.tn.value="baidu";
formname.method="get";
}
if(formname.myselectvalue.value=="1")
{
url="http://mp3.baidu.com/m";
document.search_form1.tn.value="baidump3";
document.search_form1.ct.value="134217728";
document.search_form1.lm.value="-1";
}
if(formname.myselectvalue.value=="4")
{
document.search_form1.tn.value="news";
document.search_form1.cl.value="2";
document.search_form1.rn.value="20";
url="http://news.baidu.com/ns";
}
if(formname.myselectvalue.value=="5")
{
document.search_form1.tn.value="baiduimage";
document.search_form1.ct.value="201326592";
document.search_form1.cl.value="2";
document.search_form1.lm.value="-1";
url="http://image.baidu.com/i";
}
if(formname.myselectvalue.value=="6")
{
url="http://post.baidu.com/f";
document.search_form1.tn.value="baiduPostSearch";
document.search_form1.ct.value="352321536";
document.search_form1.rn.value="10";
document.search_form1.lm.value="65536";
}
formname.action=url;
return true;
}
</script>
</head>
分类搜索的功能基本实现,现在还差最后一步,修改网站的编码类型(百度搜索编码默认支持GB2312)。如果不修改编码类型,在搜索时会出现乱码。打开web.config文件在<system.web>节点添加以下代码:
<system.web>
<globalization requestEncoding="Gb2312" responseEncoding="GB2312" fileEncoding="GB2312"/>
</system.web>