簡介 Google 開放式 WebServices 查詢接口
發表時間:2023-08-13 來源:明輝站整理相關軟件相關文章人氣:
[摘要]查找引擎之王 google 于 4/11 開放了它的查詢接口,可以讓全世界各地的 Java 以及 .NET 程序員,免費地以 WebServices 的方式,對 google 下查找指令,并且可以將...
查找引擎之王 google 于 4/11 開放了它的查詢接口,可以讓全世界各地的 Java 以及 .NET 程序員,免費地以 WebServices 的方式,對 google 下查找指令,并且可以將結果使用于自己的程序或網頁中,并且幾乎目前所有的程序語言都有 WebServices 的相關軟體包,本次介紹應該可以給大家一個簡單的 WebServices 概念 ...
查找引擎之王 google 于 4/11 開放了它的查詢接口,可以讓全世界各地的 Java 以及 .NET 程序員,免費地以 WebServices 的方式,對 google 下查找指令,并且可以將結果使用于自己的程序或網頁中。不過使用上也有限制,它一天只允許未付費的程序員查找 1000 次。要使用前,必須先向 google 注冊帳號,取得一個 32 位長度的 license key ,每次呼叫查詢時,必須帶入這個 license key 即可使用。因為 WebServices 以 WSDL (Web Service Definition Language) 的方式描述所開放的接口,走的通訊協定是 SOAP on HTTP ,所以理論上可以穿越防火墻。 Java 范例程序如下∶
import java.io.*;
import com.google.soap.search.*; // 這是 googleapi.jar ,由 google 所提供
public class SearchGoogle {
public static void main(String[] args) {
if (args.length != 2)
{
System.err.println("Usage: java SearchGoogle LicenseKey 查詢字符串 ");
System.exit(1);
}
String clientKey = args[0];
String queryString = args[1];
System.out.println(" 查詢字符串 = " + queryString);
// 產生查找對象,設定輸出編碼 (big5)
GoogleSearch s = new GoogleSearch();
s.setKey(clientKey);
s.setOutputEncoding("big5");
try
{
s.setQueryString(qyeryString);
GoogleSearchResult r = s.doSearch();
System.out.println(" 查找結果 :");
System.out.println("======================");
System.out.println(r.toString());
}
catch (GoogleSearchFault e)
{
System.out.println(" 查找失敗 ");
}
}
}
使用方法以及查詢結果(執行環境∶ Linux + JDK 1.3.1_01 ,主機位于防火墻后端)∶
java -classpath .:../lib/googleapi.jar
SearchGoogle xxxxxxxxx- 我的 LicenseKey-xxxxxxxxx LinuxFab
查詢字符串 = LinuxFab
查找結果 :
======================
{
TM = 0.048257
Q = "LinuxFab"
CT = ""
TT = ""
CATs =
{
{SE="Big5", FVN="Top/World/Chinese_Traditional/ 計算機 /Operating_Systems/Unix/Linux"}
}
Start Index = 1
End Index = 10
Estimated Total Results Number = 11500
Document Filtering = true
Estimate Correct = false
Rs =
{
[
URL = "http://bbs.ee.ntu.edu.tw/boards/Linux/21/"
Title = " ◆ 【 LinuxFab 早報】 "
Snippet = " ◆ 【 LinuxFab 早報】 . ... 5, ◇ 【 LinuxFab 本周專欄】 如何制作簡易 Floppy Liunx, linuxfab. 07/13/01. ... "
Directory Category = {SE="", FVN=""}
Directory Title = ""
Summary = ""
Cached Size = "11k"
Related information present = true
Host Name = ""
],
[
URL = "http://bbs.ee.ntu.edu.tw/boards/Linux/21/73.html"
Title = " ◇ 【 LinuxFab 早報】 - Linux 2.4.10 內核中剽竊了 ┅ "
Snippet = " ◇ 【 LinuxFab 早報】 - Linux 2.4.10 內核中 剽竊了 ┅ . 發信人 : linuxfab.bbs ... "
Directory Category = {SE="", FVN=""}
Directory Title = ""
Summary = ""
Cached Size = "10k"
Related information present = true
Host Name = "bbs.ee.ntu.edu.tw"
],
[
URL = "http://www.lslnet.com/linux/docs/linux-3033.htm"
Title = "LinuxFab: Red Hat7.0 ? 新特色 - [? 森林 - 自由 ? 件 ]"
Snippet = " ... LinuxFab: Red Hat7.0 ? 新特色 ? 森林 http://www.lslnet.com 2000 年 9 月 24 日 09:59 ... 摘自∶ http:linuxfab.cx [ ?? 窗口 ]. ... "
Directory Category = {SE="", FVN=""}
Directory Title = ""
Summary = ""
Cached Size = "5k"
Related information present = true
Host Name = ""
],
下略
當然,輸出結果不是這么雜亂, google 所提供的 api 有各種查找設定方法,例如從第幾筆開始查找、設定傳回筆數、偏好查找(避免查找 java 時傳回 咖啡 的結果) ... 等等,各位可以好好玩一玩。
如果您不想使用 google 的 library ,您也可以利用手邊的 WebServices 工具程序,自動抓取 google 所提供的 WSDL 文檔(位于∶ http://api.google.com/GoogleSearch.wsdl ),并且產生相關的 java 對象。以下以 GLUE 這套 Java WebServices 軟體包做范例∶
$ wsdl2java http://api.google.com/GoogleSearch.wsdl -p example -d example/
write file example/IGoogleSearchPort.java
write file example/GoogleSearchServiceHelper.java
write file example/ResultElement.java
write file example/DirectoryCategory.java
write file example/GoogleSearchResult.java
write file GoogleSearchService.map
GLUE 的強處在于它自動為您處理所有 XML <-> Java Object Mapping 以及 SOAP 傳輸的問題,您甚至不需要看懂 WSDL 便可直接使用 WebServices !我們現在已經將 WSDL 轉換成 Java 對象, package 名為 example ,接下來便是撰寫 Client 去連接 google ∶
import java.io.*;
import electric.registry.Registry; // 包含于 GLUE-STD.jar ,為 GLUE 標準版的 library
import example.*; //GLUE 自動為您由 WSDL 產生出來的 Java 對象
public class SearchGoogle2 {
public static void main(String[] args) {
if (args.length != 2)
{
System.err.println("Usage: java SearchGoogle LicenseKey 查詢字符串 ");
System.exit(1);
}
String clientKey = args[0];
String queryString = args[1];
System.out.println(" 查詢字符串 = " + queryString );
String url = "http://api.google.com/GoogleSearch.wsdl";
try
{
// 利用 GLUE 產生的接口,去 bind google 的 WSDL
IGoogleSearchPort searcher = (IGoogleSearchPort) Registry.bind(url , IGoogleSearchPort.class);
// 查詢
GoogleSearchResult result = searcher.doGoogleSearch
( clientKey , queryString , 0 , 1 , false , "" , false , "" , "Big5" , "Big5" );
System.out.println(" 查找結果 :");
System.out.println("======================");
System.out.println(" 回應數量∶ " + result.resultElements.length );
System.out.println(" 第一筆結果∶ ");
System.out.println(" 簡介∶ " + result.resultElements[0].summary );
System.out.println("URL ∶ " + result.resultElements[0].URL );
System.out.println(" 片段∶ " + result.resultElements[0].snippet );
System.out.println(" 標頭∶ " + result.resultElements[0].title );
System.out.println(" 主機∶ " + result.resultElements[0].hostName );
System.out.println(" 目錄∶ " + result.resultElements[0].directoryTitle );
}
catch (electric.registry.RegistryException e)
{
System.out.println(" 錯誤∶ " + e.getMessage());
}
}
}
執行結果指令如下∶
$ java -classpath .:../lib/GLUE-STD.jar:../lib/j2ee.jar:../lib/jnet.jar
SearchGoogle2 xxxxxxxxx- 我的 LicenseKey-xxxxxxxxx LinuxFab
查詢字符串 = LinuxFab
查找結果 :
======================
回應數量∶ 1
第一筆結果∶
簡介∶
URL ∶ http://bbs.ee.ntu.edu.tw/boards/Linux/21/
片段∶◆ 【 LinuxFab 早報】 . ... 5, ◇ 【 LinuxFab 本周專欄】 如何制作簡易 Floppy Liunx, linuxfab. 07/13/01. ...
標頭∶◆ 【 LinuxFab 早報】
主機∶
目錄∶
要注意的是,這只是 google beta2 版本的 WSDL ,未來接口一定會有所更動,而且遠端主機 (api.google.com) 仍然不太穩定,有時候會連不過去,有時又會出現 Internal Server Error 的錯誤。不過以上的介紹應該可以給大家一個簡單的 WebServices 概念了吧?目前幾乎所有的程序語言都有 WebServices 的相關軟體包,不論您是 PHP 網頁制作者,或是 VB 窗口程序員,應該都找到相關的軟體包,直接在您的網頁 / 程序上面撰寫 WebServices 的 Client 以及 Server 。
截稿前消息∶全球最大的網絡書店 Amazon ,也開始提供 WebServices 接口給代銷商( associates ,可以在個人網頁上放置連往 Amazon 的 icon 或連結,藉此賺取部分成交金額)使用。代銷商現在可以利用 Amazon 提供的 WebServices 撰寫 Web/Desktop 應用程序了!看來類似的 WebServices 將會逐漸在許多大站點中陸續被采用,或許 LinuxFab/SourceFab 可以考慮引進此技術,使本站成為華人 Open Source 界的網絡運算中心。