通过Yahoo+Search+API访问Web服务实现自己的搜索引擎

效果截图:
通过Yahoo+Search+API访问Web服务实现自己的搜索引擎 - lishirong - The CTO of LiShirong
 

本实例中并未实现所谓的模拟搜索引擎,因为没有较好的网络在yahoo上申请到账号,所以以后再测试了。

静态页面:yahooSearch.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 

Transitional//EN" 

"http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Yahoo! Search Web Services</title>
<script type="text/javascript">
var xmlHttp;
function createXMLHttpRequest(){
if(window.ActiveXObject){
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}else if(window.XMLHttpRequest){
xmlHttp=new XMLHttpRequest();
}
}

function doSearch(){
var url="servlet/YahooSearchGatewayServlet?"+createQueryString()+"&ts="+new Date().getTime();
createXMLHttpRequest();
xmlHttp.onreadystatechange=handleStateChange;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

function createQueryString(){
var searchString=document.getElementById("searchString").value;
searchString=escape(searchString);
var maxResultsCount=document.getElementById("maxResultCount").value;
var queryString="query="+searchString+"&results="+maxResultsCount;
return queryString;
}

function handleStateChange(){
if(xmlHttp.readyState==4){
if(xmlHttp.status==200){
parseSearchResults();
}
else{
alert("Error accessing Yahoo!search");
}
}
}

function parseSearchResults(){
var resultsDiv=document.getElementById("results");
while(resultsDiv.childNodes.length>0){
resultsDiv.removeChild(resultsDiv.childNodes[0]);
}
var allResults=xmlHttp.responseXML.getElementsByTagName("Result");
var result=null;
for(var i=0;i<allResults.length;i++){
result=allResults[i];
parseResult(result);
}
}

function parseResult(result){
var resultDiv=document.createElement("div");
var title=document.createElement("h3");
title.appendChild(document.createTextNode(getChildElementText(result,"Title")));
resultDiv.appendChild(title);
var summary=document.createTextNode(getChildElementText(result,"Summary"));
resultDiv.appendChild(summary);
resultDiv.appendChild(document.createElement("br"));
var clickHere=document.createElement("a");
clickHere.setAttribute("href",getChildElementText(result,"ClickUrl"));
clickHere.appendChild(document.createTextNode(getChildElementText(result,"Url")));
resultDiv.appendChild(clickHere);
document.getElementById("results").appendChild(resultDiv);
}

function getChildElementText(parentNode,childTagName){
var childTag=parentNode.getElementsByTagName(childTagName);
return childTag[0].firstChild.nodeValue;
}
</script>
</head>
<body bgcolor="#CCFFFF">
<h1>通过Yahoo网关搜索并返回结果</h1>
<form action="#">
请填写要搜索的内容:<input type="text" id="searchString"/>
<br/><br/>
显示结果的条数:
<select id="maxResultCount">
<option value="1">1</option>
<option value="10">10</option>
<option value="25">25</option>
<option value="50">50</option>
</select>
<br/><br/>
<input type="button" value="提交" οnclick="doSearch();"/>
</form>
<h2>搜索结果:<h2>
<div id="results"/>
</body>
</html>

处理页面:YahooSearchGatewayServlet.java

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 

Transitional//EN" 

"http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Yahoo! Search Web Services</title>
<script type="text/javascript">
var xmlHttp;
function createXMLHttpRequest(){
if(window.ActiveXObject){
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}else if(window.XMLHttpRequest){
xmlHttp=new XMLHttpRequest();
}
}

function doSearch(){
var url="servlet/YahooSearchGatewayServlet?"+createQueryString()+"&ts="+new Date().getTime();
createXMLHttpRequest();
xmlHttp.onreadystatechange=handleStateChange;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

function createQueryString(){
var searchString=document.getElementById("searchString").value;
searchString=escape(searchString);
var maxResultsCount=document.getElementById("maxResultCount").value;
var queryString="query="+searchString+"&results="+maxResultsCount;
return queryString;
}

function handleStateChange(){
if(xmlHttp.readyState==4){
if(xmlHttp.status==200){
parseSearchResults();
}
else{
alert("Error accessing Yahoo!search");
}
}
}

function parseSearchResults(){
var resultsDiv=document.getElementById("results");
while(resultsDiv.childNodes.length>0){
resultsDiv.removeChild(resultsDiv.childNodes[0]);
}
var allResults=xmlHttp.responseXML.getElementsByTagName("Result");
var result=null;
for(var i=0;i<allResults.length;i++){
result=allResults[i];
parseResult(result);
}
}

function parseResult(result){
var resultDiv=document.createElement("div");
var title=document.createElement("h3");
title.appendChild(document.createTextNode(getChildElementText(result,"Title")));
resultDiv.appendChild(title);
var summary=document.createTextNode(getChildElementText(result,"Summary"));
resultDiv.appendChild(summary);
resultDiv.appendChild(document.createElement("br"));
var clickHere=document.createElement("a");
clickHere.setAttribute("href",getChildElementText(result,"ClickUrl"));
clickHere.appendChild(document.createTextNode(getChildElementText(result,"Url")));
resultDiv.appendChild(clickHere);
document.getElementById("results").appendChild(resultDiv);
}

function getChildElementText(parentNode,childTagName){
var childTag=parentNode.getElementsByTagName(childTagName);
return childTag[0].firstChild.nodeValue;
}
</script>
</head>
<body bgcolor="#CCFFFF">
<h1>通过Yahoo网关搜索并返回结果</h1>
<form action="#">
请填写要搜索的内容:<input type="text" id="searchString"/>
<br/><br/>
显示结果的条数:
<select id="maxResultCount">
<option value="1">1</option>
<option value="10">10</option>
<option value="25">25</option>
<option value="50">50</option>
</select>
<br/><br/>
<input type="button" value="提交" οnclick="doSearch();"/>
</form>
<h2>搜索结果:<h2>
<div id="results"/>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值