六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 19|回复: 0

Google Search API & JSON

[复制链接]

升级  74%

9

主题

9

主题

9

主题

童生

Rank: 1

积分
37
 楼主| 发表于 2013-2-3 12:38:21 | 显示全部楼层 |阅读模式
Google Search API
=================

   Google Search API give us an occasion to access to google search results. These results will be given in the JSON (JavaScript Object Notation) format.JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.
    This development needs an input of 7 librairies which are:
         - json-lib-2.4-jdk15.jar
         - ezmorph-1.0.2.jar
         - commons-lang-2.6.jar
         - commons-lang-2.6-javadoc.jar
         - commons-lang-2.6-sources.jar
         - json-simple-1.1-bundle.jar
         - json-taglib-0.4.1.jar

These librairies  needs to be all used at same time or some exceptions might occure.

Here are the codes to get the number of url in which the searched name is, and the text in the web page which contains the name.

package com.google.pack1;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.net.MalformedURLException;import java.net.URL;import java.net.URLConnection;import java.util.ArrayList;import atg.taglib.json.util.JSONArray;import atg.taglib.json.util.JSONException;import atg.taglib.json.util.JSONObject;import atg.taglib.json.util.JSONTokener;/** * This class is used to test some algorithm in the POLYPHONET:An Advanced Social Network paper * @author Pascal */public class Polyphonet {//Is set for the JSAI casepublic static int k = 30;//co- occurence indexpublic float f = 0;public String searchURL = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&rsz=8&hl=en&q="; public static void main(String[] args) {Polyphonet p = new Polyphonet();System.out.println(p.coocFunction(80, 20, 100));p.googleHit("Bujumbura");p.googleTop("Bujumbura", k);}/** * This method is called to calculate the co-occurence index f * @param nx: result for x name * @param ny: result for y name * @param nxy: result for x and y name * @return f */public float coocFunction(int nx,int ny,int nxy){float min=0;//Determine the minimal valueif(nx>ny){min = ny;}else{min = nx;}//calculate the f functionif(nx>k && ny>k){f = nxy/min; }else{f=0;}return f;}/** *  * @param name: searched name * @return: Number of hits retrieved by a given query */public int googleHit(String name){name = name.replace(" ", "%20");String sURL = searchURL +name;try {//create a url objectURL url = new URL(sURL);System.out.println("the url is: "+sURL);//create a url connection objectURLConnection conn = url.openConnection();System.out.println("The url is well connected");//get the input streamInputStream isr = conn.getInputStream();BufferedReader br = new BufferedReader(new InputStreamReader(isr));//result of the searchString result = br.readLine();System.out.println(result);//create a JSON objectJSONObject jsResult = new JSONObject(result).getJSONObject("responseData").getJSONObject("cursor");System.out.println(jsResult.toString());//get the number of resultint n = jsResult.getInt("estimatedResultCount");System.out.println("The estimated result is: "+n);return n;} catch (MalformedURLException e) {// TODO Auto-generated catch blocke.printStackTrace();System.out.println("Please verify the URL "+sURL);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();System.out.println("The url didn't succeed to get connected, please try again");} catch (JSONException e) {// TODO Auto-generated catch blocke.printStackTrace();}return 0;}/** * This method will search the text in the url found * @param name: searched name * @param k: Quantity or number set for the JSAI case in research * @return: D arrayList which contain all the found text */public ArrayList<String> googleTop(String name, int k){name = name.replace(" ", "%20");String sURL = searchURL +name;try {//create a url objectURL url = new URL(sURL);System.out.println("the url is: "+sURL);//create a url connection objectURLConnection conn = url.openConnection();System.out.println("The url is well connected");//get the input streamInputStream isr = conn.getInputStream();BufferedReader br = new BufferedReader(new InputStreamReader(isr));//result of the searchString result = br.readLine();//create a JSON objectJSONArray jsaResult = new JSONObject(result).getJSONObject("responseData").getJSONArray("results");//create an arrayListArrayList<String> D = new ArrayList<String>();//get the content of k first resultsfor(int i = 0; i<jsaResult.size()&&i<k; i++){//Get each index as an objectJSONObject jsCont = jsaResult.getJSONObject(i);String content = jsCont.getString("content");System.out.println("The content is "+ content);D.add(content);}return D;} catch (MalformedURLException e) {// TODO Auto-generated catch blocke.printStackTrace();System.out.println("Please verify the URL "+sURL);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();System.out.println("The url didn't succeed to get connected, please try again");} catch (JSONException e) {// TODO Auto-generated catch blocke.printStackTrace();}return null;}}


For more informations, these are the links:
     - http://code.google.com/apis/websearch/docs/reference.html#_intro_fonje

     - http://www.json.org/
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表