1、下载googleapi.jar,导入classpath路径,获取访问许可证号
2、代码为:
import com.google.soap.search.GoogleSearch;
import com.google.soap.search.GoogleSearchResult;
import com.google.soap.search.GoogleSearchFault;
import com.google.soap.search.GoogleSearchResultElement;
public class GoogleClient {
public static void main(String[] args) {
String keyword = "Java";
String clientKey = "00000000000000000000000000";//请将其换成自己的许可证号
GoogleSearch gs = new GoogleSearch();
gs.setKey(clientKey);
gs.setQueryString(keyword);
gs.setLanguageRestricts("lang_zh-CN");
gs.setStartResult(10);
try {
GoogleSearchResult gsr = gs.doSearch();
GoogleSearchResultElement[] gsreList = gsr.getResultElements();
for (int i = 0; i < gsreList.length; i++) {
GoogleSearchResultElement gsre = gsreList;
System.out.println("Title : " + gsre.getTitle());
System.out.println("URL : " + gsre.getURL());
System.out.println("Snippet: " + gsre.getSnippet());
System.out
.println("----------------------------------------------------------");
}
} catch (GoogleSearchFault gsf) {
System.out.println("The call to the Google Web APIs failed: "
+ gsf.toString());
}
}
}
[此贴子已经被作者于2010-12-14 09:27:57编辑过]