Google Web Search


这学期和老师做的项目要先进行google search, 找了一下,google web search api刚好符合我所需要的各种要求( https://developers.google.com/web-search/docs/?csw=1#fonje_snippets_java),但是Deprecated了。倒不是不可以用,但是得到的结果确实有点奇怪,估计的结果数目和显示的数目完全不一样。。。


于是开始研究custom search JSON/Atom API。还没太明白...

这里算是做一个记录吧。

package searchTeat1;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.*;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.json.JSONArray;
import org.json.JSONObject;

public class test1 {
	
	public static void main(String[] args) throws Exception, Exception {		
/		
//		String google = "http://www.google.com/search?q=";
//		String search = "Barack Obama  Honolulu, Hawaii";
//		String charset = "UTF-8";
//		String userAgent = "ExampleBot 1.0 (+http://example.com/bot)"; // Change this to your company's name and bot homepage!
//	
//		Elements links = Jsoup.connect(google + URLEncoder.encode(search, charset)).userAgent(userAgent).get().select("li.g>h3>a");
//	
//		for (Element link : links) 
//		{
//		    String title = link.text();
//		    String url = link.absUrl("href"); // Google returns URLs in format "http://www.google.com/url?q=<url>&sa=U&ei=<someKey>".
//		    url = URLDecoder.decode(url.substring(url.indexOf('=') + 1, url.indexOf('&')), "UTF-8");
//	
//		    if (!url.startsWith("http")) {
//		        continue; // Ads/news/etc.
//		    }
//	
//		    System.out.println("Title: " + title);
//		    System.out.println("URL: " + url);
//		    System.out.println("=============================\n");
//		}
		/
		// The request also includes the userip parameter which provides the end
		// user's IP address. Doing so will help distinguish this legitimate
		// server-side traffic from traffic which doesn't come from an end-user.
		//strQuery = "q=George+Bush+"new+haven"&userip=10.109.3.60";
		//function1
		String entity = "George+Bush";
		String attrValue = "new+haven";
		
		//everyday 900
		//function2
		String strQuery = String.format("q=%s+"%s"&userip=10.109.3.60", entity, attrValue);
		
		URL url = new URL(
		    "https://ajax.googleapis.com/ajax/services/search/web?v=1.0&" + strQuery);
		URLConnection connection = url.openConnection();
		connection.addRequestProperty("Referer", "www.google.com");

		String line;
		StringBuilder builder = new StringBuilder();
		BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
		while((line = reader.readLine()) != null) {
		 builder.append(line);
		}

		JSONObject json = new JSONObject(builder.toString());
		// now have some fun with the results...
		
		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"));  
            System.out.println("\n");
        }
	
	
	
	}

}

过两天代码应该又面目全非了。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值