java调用Google Search API

1、方式一

package com.kute.allutils.callgoolesearchapi;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLEncoder;

/**
 * 用java来调用 Goole Serach API
 * 此种方式的结果不便于阅读:
 * {"responseData": {"results":[{"GsearchResultClass":"GwebSearch","unescapedUrl":"http://docs.oracle.com/javase/tutorial/","url":"http://docs.oracle.com/javase/tutorial/","visibleUrl":"docs.oracle.com","cacheUrl":"http://www.google.com/search?q\u003dcache:xzc8wI2QtQsJ:docs.oracle.com","title":"The \u003cb\u003eJava\u003c/b\u003e鈩?Tutorials - Oracle Documentation","titleNoFormatting":"The Java鈩?Tutorials - Oracle Documentation","content":"Tutorials and reference guides for the \u003cb\u003eJava\u003c/b\u003e Programming聽\u003cb\u003e...\u003c/b\u003e"},{"GsearchResultClass":"GwebSearch","unescapedUrl":"http://docs.oracle.com/javase/tutorial/java/","url":"http://docs.oracle.com/javase/tutorial/java/","visibleUrl":"docs.oracle.com","cacheUrl":"http://www.google.com/search?q\u003dcache:1eE2Wha2RKkJ:docs.oracle.com","title":"Trail: Learning the \u003cb\u003eJava\u003c/b\u003e Language (The \u003cb\u003eJava\u003c/b\u003e鈩?Tutorials)","titleNoFormatting":"Trail: Learning the Java Language (The Java鈩?Tutorials)","content":"Trail: Learning the Java Language. This   trail covers the fundamentals of聽\u003cb\u003e...\u003c/b\u003e"},{"GsearchResultClass":"GwebSearch","unescapedUrl":"http://www.tutorialspoint.com/java/","url":"http://www.tutorialspoint.com/java/","visibleUrl":"www.tutorialspoint.com","cacheUrl":"http://www.google.com/search?q\u003dcache:aYYZYo3jxBMJ:www.tutorialspoint.com","title":"\u003cb\u003eJava Tutorial\u003c/b\u003e - Tutorials Point","titleNoFormatting":"Java Tutorial - Tutorials Point","content":"\u003cb\u003eJava Tutorial\u003c/b\u003e for Beginners - Learning Java in simple and easy steps : A   beginner\u0026#39;s tutorial containing complete knowledge of Java Syntax Object   Oriented聽\u003cb\u003e...\u003c/b\u003e"},{"GsearchResultClass":"GwebSearch","unescapedUrl":"http://www.learnjavaonline.org/","url":"http://www.learnjavaonline.org/","visibleUrl":"www.learnjavaonline.org","cacheUrl":"http://www.google.com/search?q\u003dcache:n3d4cY8WpeAJ:www.learnjavaonline.org","title":"Free Interactive \u003cb\u003eJava Tutorial\u003c/b\u003e: Learn Java","titleNoFormatting":"Free Interactive Java Tutorial: Learn Java","content":"LearnJavaOnline.org is a free interactive \u003cb\u003eJava tutorial\u003c/b\u003e for people who want to   learn Java, fast."}],"cursor":{"resultCount":"14,100,000","pages":[{"start":"0","label":1},{"start":"4","label":2},{"start":"8","label":3},{"start":"12","label":4},{"start":"16","label":5},{"start":"20","label":6},{"start":"24","label":7},{"start":"28","label":8}],"estimatedResultCount":"14100000","currentPageIndex":0,"moreResultsUrl":"http://www.google.com/search?oe\u003dutf8\u0026ie\u003dutf8\u0026source\u003duds\u0026start\u003d0\u0026hl\u003den\u0026q\u003djava+tutorial","searchResultTime":"0.10"}}, "responseDetails": null, "responseStatus": 200}
 * @author kute
 * @date 2013年10月28日
 */
public class Method_1 {

	public static void main(String[] args) throws Exception {
		String address = "http:ajax.googleapis.com/ajax/services/search/web?v=1.0&q=";
		String query = "java tutorial";
		String charset = "UTF-8";
	 
		URL url = new URL(address + URLEncoder.encode(query, charset));
		
		BufferedReader in = new BufferedReader(new InputStreamReader(
				url.openStream()));
		String str;
	 
		while ((str = in.readLine()) != null) {
			System.out.println(str);
		}
	 
		in.close();
	}

}

2、方式二

package com.kute.allutils.callgoolesearchapi;

import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.net.URLEncoder;
import java.util.List;

import com.google.gson.Gson;

/**
 * 此种方式的结果返回url,这样只返回了4中结果,但是我们需要更多,在address中增加一个参数,start,类似与这样:
 * http://ajax.googleapis.com/ajax/services/search/web?v=1.0&start=4&q=
 * 上面的start参数指定为4,结果就显示第5-8条记录,以此类推
 * 
 * total: 4 
 * Title: <b>ProgramCreek</b>.com URL:
 * http://www.programcreek.com/
 * 
 * Title: Top 8 Diagrams for Understanding Java - <b>ProgramCreek</b>.com URL:
 * http://www.programcreek.com/2013/09/top-8-diagrams-for-understanding-java/
 * 
 * Title: Top 10 Methods for Java Arrays - <b>ProgramCreek</b>.com URL:
 * http://www.programcreek.com/2013/09/top-10-methods-for-java-arrays/
 * 
 * Title: Eclipse JDT Tutorials - <b>ProgramCreek</b>.com URL:
 * http://www.programcreek
 * .com/2011/01/best-java-development-tooling-jdt-and-astparser-tutorials/
 * 
 * @author kute
 * @date 2013年10月28日
 */
public class Method_2 {

	public static void main(String[] args) throws Exception {
		String address = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=";
		String query = "programcreek";
		String charset = "UTF-8";

		URL url = new URL(address + URLEncoder.encode(query, charset));
		Reader reader = new InputStreamReader(url.openStream(), charset);
		GoogleResults results = new Gson()
				.fromJson(reader, GoogleResults.class);

		int total = results.getResponseData().getResults().size();
		System.out.println("total: " + total);

		// Show title and URL of each results
		for (int i = 0; i <= total - 1; i++) {
			System.out.println("Title: "
					+ results.getResponseData().getResults().get(i).getTitle());
			System.out.println("URL: "
					+ results.getResponseData().getResults().get(i).getUrl()
					+ "\n");
		}
	}

}

class GoogleResults {

	private ResponseData responseData;

	public ResponseData getResponseData() {
		return responseData;
	}

	public void setResponseData(ResponseData responseData) {
		this.responseData = responseData;
	}

	public String toString() {
		return "ResponseData[" + responseData + "]";
	}

	static class ResponseData {
		private List<Result> results;

		public List<Result> getResults() {
			return results;
		}

		public void setResults(List<Result> results) {
			this.results = results;
		}

		public String toString() {
			return "Results[" + results + "]";
		}
	}

	static class Result {
		private String url;
		private String title;

		public String getUrl() {
			return url;
		}

		public String getTitle() {
			return title;
		}

		public void setUrl(String url) {
			this.url = url;
		}

		public void setTitle(String title) {
			this.title = title;
		}

		public String toString() {
			return "Result[url:" + url + ",title:" + title + "]";
		}
	}
}

3、方式三

package com.kute.allutils.callgoolesearchapi;

import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.net.URLEncoder;

import com.google.gson.Gson;

/**
 * 此种方式是将搜索置于循环中,以便获得更多的结果
 * 你得注意,Goole是不允许在特定的时间内发出太多的请求
 * @author kute
 * @date 2013年10月28日
 */
public class Method_3 {

	public static void main(String[] args) throws Exception {
		for (int i = 0; i < 20; i = i + 4) {
			String address = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&start="
					+ i + "&q=";

			String query = "Programcreek";
			String charset = "UTF-8";

			URL url = new URL(address + URLEncoder.encode(query, charset));
			Reader reader = new InputStreamReader(url.openStream(), charset);
			GoogleResults results = new Gson().fromJson(reader,
					GoogleResults.class);

			// Show title and URL of each results
			for (int m = 0; m <= 3; m++) {
				System.out.println("Title: "
						+ results.getResponseData().getResults().get(m)
								.getTitle());
				System.out.println("URL: "
						+ results.getResponseData().getResults().get(m)
								.getUrl() + "\n");
			}
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值