一简单的Google API搜索例子 |
首先看一个Java代码的例子 需要两个jar的支持 commons-httpclient.jar commons-codec-1.3.jar view plaincopy to clipboardprint? import java.io.UnsupportedEncodingException; import java.URLEncoder; import s.httpclient.DefaultHttpMethodRetryHandler; import s.httpclient.HttpClient; import s.httpclient.HttpStatus; import hods.GetMethod; import s.httpclient.params.HttpMethodParams; import org.json.JSONArray; import org.json.JSONObject; public class GoogleQuery { public GoogleQuery() throws UnsupportedEncodingException { String query =&de("AOP示例+site:blog.csdn/zhjb1025", "UTF-8");//站内搜索 //String query =&de("AOP示例", "UTF-8"); for (int i = 0; i < 3; i++) { 仁爱英语七年级上册教案 makeQuery("/ajax/rvices/arch/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); uteMethod(getMethod); Params().tParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); int statusCode =&uteMethod(getMethod); if (statusCode != HttpStatus.SC_OK) { println("Method failed: " +&StatusLine()); apu是什么 } byte[] responBody =&ResponBody(); String respon = new String(responBody, "UTF-8"); JSONObject json = new JSONObject(respon); System.out.println("Total results = " +&JSONObject("responData") .getJSONObject("cursor").getString( "estimatedResultCount")); JSONArray ja =&JSONObject("responData").getJSONArray( "results"); System.out.println(" Results:"); for (int i = 0; i < ja.length(); i++) { 初一英语单词表 JSONObject j =&JSONObject(i); System.out.String("titleNoFormatting")); System.out.String("url")); 中英网 System.out.("content")); } } catch (Exception e) { println("Something went&"); e.printStackTrace(); } System.out.println("--------------------------------------------"); } public static void main(String args[]) { try { new GoogleQuery(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } } 再来一个js的例子 view plaincopy to clipboardprint? <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "www.w3/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="www.w3/1999/xhtml"> <head> <title>My Google AJAX Search API Application</title> <mce:script src="/jsapi?key=你申请的key" mce_src="/jsapi?key=你申请的key" type="text/javascript"></mce:script> <mce:script language="Javascript" type="text/javascript"><!-- google.load("arch", "1"); function OnLoad() { // Create a arch control var archControl = new google.arch.SearchControl(); buy// Add in a full t of archers var localSearch = new google.arch.LocalSearch(); archControl.addSearcher(localSearch); archControl.addSearcher(new google.arch.WebSearch()); archControl.addSearcher(new google.arch.VideoSearch()); archControl.addSearcher(new google.arch.BlogSearch()); // Set the Local Search center point //localSearch.tCenterPoint("New York, NY"); walking on sunshine// Tell the archer to draw itlf and tell it where to attach archControl.ElementById("archcontrol")); // Execute an inital arch ute("java"); } google.tOnLoadCallback(OnLoad); decorate// --></mce:script> </head> <body> <div id="archcontrol"></div> </body> </html> |
本文发布于:2023-06-09 03:58:32,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/fan/90/138831.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |