分享

中文Google论坛 - PageRank, AdWords, AdSense, Froogle--使用Google的WebService[转帖]

 karoc 2006-04-30
 使用Google的Web Service [转帖]
http://www./Info/85/Info30450/
 
一、简介 
Google搜索引擎提供了基于SOAP的Web Service。这意味着不同的开发语言、开发环境都能够使用这种服务,另外,Google为了简化Java程序员的开发,它还提供了一套Java API接口,用于访问Web Serivce,这使得开发一套支持Google搜索功能的程序变得十分容易。开发人员可以将Google的搜索功能嵌入到他们的应用程序当中。本文将介绍如何使用这些Java API,以及如何使用google的Web Service。 
目前,Google的API还是处于Beta测试阶段,因此,在API的使用上还有一些限制。例如,使用这些API需要申请一个账号。对于免费申请的账号,为了防止开发人员不正当的使用,限制每个账号、每天最多只能查询1000次。目前,Google还没有提出正式的运营模式和收费方式。 
Google API目前主要提供三种服务,分别如下: 
1)搜索服务。用户提交一个搜索内容请求,Google服务器将查找超过20亿的网页内容,并且将满足用户需求的目标返回给用户,一般来说这种处理只需要几秒的时间。 
2)缓存服务(Cache)。用户提交一个URL,Google服务器将返回搜索器访问该URL的最新记录。这样,用户很容易复用Google的搜索成果。 
3)拼写检查。该功能用于检查用户的查询请求是否有效。用户提交一个检查内容,Google服务器将返回有效的、最接近该内容的查询请求,返回的查询请求将符合Google的查询规则。 

 

二、相关准备工作 
为了开发基于Java的程序,我们需要作如下准备工作。 
1)建立Java开发环境,Java SDK 1.3.1 或更新的版本。相关地址http:.//java./J2SE 
2)下载Google API的Jar文件,Googleapi.jar。开发人员可以到http://www.google.com/apis下载。 
3)申请Google访问账号,目前Google支持免费申请的方式,用户需要使用一个email账号来获取新账号。注意,目前免费账号支持每天1000条的最大查询量。申请地址:http://www.google.com/apis。 
4)(可选) 如果用户需要通过Java程序直接发送SOAP请求,开发人员需要下载相关的Java 软件包,JAXM。下载地址:http://java./xml 

三、SOAP的使用 
Google提供了基于SOAP的Web Service,因此用户可以向Google服务器提交SOAP的查询请求,而后Google服务器将处理这些请求,并返回SOAP格式的查询结果。以下就是一个查询请求和查询结果。 
查询:例如,有一个查询请求,请求类型为搜索,查询的内容为“world cup”,账号为“123456789”,如下为该查询的一个例子。 
<?xml version=‘‘1.0‘‘ encoding=‘‘UTF-8‘‘?> 
<SOAP-ENV:Envelope 
xmlns:SOAP-ENV="http://schemas./soap/envelope/" 
xmlns:xsi="http://www./1999/XMLSchema-instance" 
xmlns:xsd="‘ target=_blank>http://www./1999/XMLSchema"> 
<SOAP-ENV:Body> 
<ns1: doGoogleSearch xmlns:ns1="urn:GoogleSearch" 
SOAP-ENV:encodingStyle= 
"‘ target=_blank>http://schemas./soap/encoding/"> 
<key xsi:type="xsd:string">123456789</key> 
<q xsi:type="xsd:string">"World Cup"</q> 
<start xsi:type="xsd:int">0</start> 
<maxResults xsi:type="xsd:int">10</maxResults> 
<filter xsi:type="xsd:boolean">true</filter> 
<restrict xsi:type="xsd:string"></restrict> 
<safeSearch xsi:type="xsd:boolean">false</safeSearch> 
<lr xsi:type="xsd:string"></lr> 
<ie xsi:type="xsd:string">latin1</ie> 
<oe xsi:type="xsd:string">latin1</oe> 
</ns1: doGoogleSearch > 
</SOAP-ENV:Body> 
</SOAP-ENV:Envelope> 
查询结果:如果我们执行以上查询,可以得到如下的查询结果。其中,查询结果总共有约2660000记录,使用时间为0.125012秒。Item标记代表一条查询结果,在第一条查询结果中,网站的URL为http://www.。本例子仅仅列举了一条查询结果。 

<?xml version="1.0" encoding="UTF-8"?> 
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas./soap/envelope/" 
xmlns:xsi="http://www./1999/XMLSchema-instance" 
xmlns:xsd="‘ target=_blank>http://www./1999/XMLSchema"> 
<SOAP-ENV:Body> 
<ns1:doGoogleSearchResponse 
xmlns:ns1="urn:GoogleSearch" 
SOAP-ENV:encodingStyle="‘ target=_blank>http://schemas./soap/encoding/"> 
<return xsi:type="ns1:GoogleSearchResult"> 
<documentFiltering xsi:type="xsd:boolean">false</documentFiltering> 
<estimatedTotalResultsCount xsi:type="xsd:int"> 
2660000</estimatedTotalResultsCount> 
<directoryCategories 
xmlns:ns2="http://schemas./soap/encoding/" 
xsi:type="ns2:Array" 
ns2:arrayType="ns1:DirectoryCategory[0]"> 
</directoryCategories> 
<searchTime xsi:type="xsd:double">0.125012</searchTime> 
<resultElements 
xmlns:ns3="http://schemas./soap/encoding/" 
xsi:type="ns3:Array" ns3:arrayType="ns1:ResultElement[10]"> 

<item xsi:type="ns1:ResultElement"> 
<cachedSize xsi:type="xsd:string">10k</cachedSize> 
<hostName xsi:type="xsd:string"/> 
<snippet xsi:type="xsd:string"></snippet> 
<directoryCategory xsi:type="ns1:DirectoryCategory"> 
<specialEncoding xsi:type="xsd:string"/> 
</directoryCategory> 
<relatedInformationPresent xsi:type="xsd:boolean"> 
true 
</relatedInformationPresent> 
<summary xsi:type="xsd:string"> 
The official site from FIFA, made by Yahoo. Daily news updates and loads of relevant information. 
</summary> 
<URL xsi:type="xsd:string"> 
http://www. 
</URL> 
<title xsi:type="xsd:string"> 
2002 FIFA <b>World</b>b>Cup</b>(tm) 
</title> 
</item> 
... 
</resultElements> 
<endIndex xsi:type="xsd:int">10</endIndex> 
<searchTips xsi:type="xsd:string"/> 
<searchComments xsi:type="xsd:string"/> 
<startIndex xsi:type="xsd:int">1</startIndex> 
<estimateIsExact xsi:type="xsd:boolean">false</estimateIsExact> 
<searchQuery xsi:type="xsd:string"> 
"science fiction" 
</searchQuery> 
</return> 
</ns1:doGoogleSearchResponse> 
</SOAP-ENV:Body> 
</SOAP-ENV:Envelope> 


四、使用Google API 
Google为了提高开发人员的开发效率,提供了一套基于Java的API,用于直接访问Google服务器。该API包装了Google的Web Service,使用起来比Web Service更加方便。该API 包主要包括如下几个类: 
GoogleSearch :该类封装了对于Google服务器的访问,支持搜索和缓存(cache)功能。 
GoogleSearchDirectoryCategory :表示Google中的一个分类目录 
GoogleSearchFault :该类为Exception的一个子类,用于表示API使用的错误。 
GoogleSearchResult :该类封装了搜索的结果。 
GoogleSearchResultElement:该类表示搜索结果中的每一条记录。 

如下是一个例子的源代码,用于一个简单查询,查询请求为”world Cup”,查询的账号为”123456789”。该例子将把查询结果打印出来。如果用户需要近一步解析查询结果,可以使用GoogleSearchResult和GoogleSearchResultElement类。 

import com.google.soap.search.*; 
import java.io.*; 

public class GoogleAPITest { 

public static void main(String[] args) { 

String clientKey="123456789"; 
String query="Word Cup"; 
// Create a Google Search object, set our authorization key 
GoogleSearch s = new GoogleSearch(); 
s.setKey(clientKey); 
try { 
s.setQueryString(query); 
GoogleSearchResult r = s.doSearch(); 
System.out.println("Google Search Results:"+t.toString()); 
} catch (GoogleSearchFault f) { 
System.out.println("The call to the Google Web APIs failed:"+f.toString()); 


五、总结 
Google搜索引擎提供了搜索、缓存和拼写简单功能,通过Web Service可以在不同的平台、不同的语言使用,为了简化SOAP的开发,Google还提供了包装了SOAP服务的Java API,这也大大提高了Java程序员的开发效率。随着搜索内容、形式的丰富,更多的开发人员可以将这种技术运用在自己应用程序中,扩展应用程度的功能。 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多