首先看一个Java代码的例子
需要两个jar的支持
commons-httpclient.jar
commons-codec-1.3.jar
import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.params.HttpMethodParams; import org.json.JSONArray; import org.json.JSONObject; public class GoogleQuery { public GoogleQuery() throws UnsupportedEncodingException { String query = URLEncoder.encode("AOP示例+site:blog.csdn.net/zhjb1025", "UTF-8");//站内搜索 //String query = URLEncoder.encode("AOP示例", "UTF-8"); for (int i = 0; i < 3; i++) { makeQuery("http://ajax.googleapis.com/ajax/services/search/web?start=" + i * 8 + "&rsz=large&v=1.0&q=" + query); } } private void makeQuery(String query) { try { HttpClient httpClient = new HttpClient(); GetMethod getMethod = new GetMethod(query); httpClient.executeMethod(getMethod); getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); int statusCode = httpClient.executeMethod(getMethod); if (statusCode != HttpStatus.SC_OK) { System.err.println("Method failed: " + getMethod.getStatusLine()); } byte[] responseBody = getMethod.getResponseBody(); String response = new String(responseBody, "UTF-8"); JSONObject json = new JSONObject(response); System.out.println("Total results = " + json.getJSONObject("responseData") .getJSONObject("cursor").getString( "estimatedResultCount")); JSONArray ja = json.getJSONObject("responseData").getJSONArray( "results"); System.out.println(" Results:"); for (int i = 0; i < ja.length(); i++) { JSONObject j = ja.getJSONObject(i); System.out.println(j.getString("titleNoFormatting")); System.out.println(j.getString("url")); System.out.println(j.get("content")); } } catch (Exception e) { System.err.println("Something went wrong..."); e.printStackTrace(); } System.out.println("--------------------------------------------"); } public static void main(String args[]) { try { new GoogleQuery(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } }
再来一个js的例子
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>My Google AJAX Search API Application</title> <mce:script src="http://www.google.com/jsapi?key=你申请的key" mce_src="http://www.google.com/jsapi?key=你申请的key" type="text/javascript"></mce:script> <mce:script language="Javascript" type="text/javascript"><!-- google.load("search", "1"); function OnLoad() { // Create a search control var searchControl = new google.search.SearchControl(); // Add in a full set of searchers var localSearch = new google.search.LocalSearch(); searchControl.addSearcher(localSearch); searchControl.addSearcher(new google.search.WebSearch()); searchControl.addSearcher(new google.search.VideoSearch()); searchControl.addSearcher(new google.search.BlogSearch()); // Set the Local Search center point //localSearch.setCenterPoint("New York, NY"); // Tell the searcher to draw itself and tell it where to attach searchControl.draw(document.getElementById("searchcontrol")); // Execute an inital search searchControl.execute("java"); } google.setOnLoadCallback(OnLoad); // --></mce:script> </head> <body> <div id="searchcontrol">Loading...</div> </body> </html>