line date

singles groups in san francisco

assyrian singles

online college dating

girl friend finder

singles in your area

online dating atlanta

fettish clubs

married woman sex

cybersex

live web girls

cascade singles

monmouth county nj singles

personals us

beautiful women russian

sex oral

datingservices

adultfriend find

ocean state singles

hot phone dating

cheap sex chat lines

date dating free online

columbia singles

older sex woman

wife cheating pictures

lady looking for sex

asian match maker

singlesmatching

lawton singles

exhibitionism com

rome singles

friend finder

usa personals

arab personals

sex geschichte

sex personals site

personals reviews

cheating spanish wives

bodybuilding singles

sun personals

dating swinger

girls looking for sex

match dating canada

local sex personals

houston christian singles

new haven singles

onlin chat

singles females

online dating for kids

www black girls com

office sexual encounters

cheating wife pics

dating african women

film x gratuit

yahoo matchmaker

christian dating london

online dating usa

singles in boise idaho

exposed housewives

www chat

sexkontakte

women dating uk

nake russian

kokeshi dvd

cedar rapids singles

women dating site

live singles

gay dating websites

indian online dating

gay hook up sites

list adult

westport singles

best dating services

wives couples

miami florida escort

solid single

chat room sexual

scholarships for single mothers

arkansas personals

wifes site

sex websites

adult

sex in fresno

findsex

frien finder

parents for dating

sexual encounters with ghosts

swinger by

personals usa

nd singles

online dating china

knox singles

cruise for single people

delhi women seeking men

cheating housewives

yahoopersonals xom

iranian personals

cam girls live chat

singles searching

sex yu


Using Google Search API from Java

Recently I’ve discovered ability to search in Google from Java program in a way different from brute “crawling”. And I’ve found out two options:

With this API you can issue search requests to Google’s index of billions of web pages and receive results as structured data, access information in the Google cache, and check the spelling of words. Google SOAP Search API is implemented as a web service and all you need is just get WSDL and call remote methods through SOAP protocol. Usage of this service is limited to 1000 requests per day.

But as you can read on SOAP API site

As of December 5, 2006, we are no longer issuing new API keys for the SOAP Search API. Developers with existing SOAP Search API keys will not be affected.

So you aren’t able to get a new API key (I found the old one in Internet). And Google encourages you to use the AJAX Search API, which is described below.

Example of code that perform search request and show obtained results:

import com.google.soap.search.*;
import java.io.*;
public class SimpleGoogleDemo {
	public static void main(String[] args) {
		// Create a Google Search object, set our authorization key
		GoogleSearch s = new GoogleSearch();
		String clientKey="xxxxxxxxxxxxxxxxxxxxxxxxxxxx";
		s.setKey(clientKey);
		s.setQueryString("vera.org.ua");
		GoogleSearchResult result = null;
		// Depending on user input, do search or cache query, then print out result
		try {
			result = s.doSearch();
		}
		catch (GoogleSearchFault f) {
			System.out.println("The call to the Google Web APIs failed:");
			System.out.println(f.toString());
		}    // if we made it here, the search went through
		System.out.println("Google Search Results:");
		System.out.println("======================");
		if (result!= null)
			System.out.println(result.toString());
	}
}

To run this code you need to have googleapi.jar in your classpath. This jar file contains very useful and easy-to-use class com.google.soap.search.GoogleSearch, which does all search work.

Google AJAX API

Usage of this unlimited and you can make as many request as you want. Despite the fact that Google in this API FAQ say “The Google AJAX Search API is currently available only for websites.” ability to use it exists :-)

Example of code that perform search request and show obtained results.

import java.io.*;
import java.net.*;
 
public class GoogleAJAXSearchAPI {    
 
	private static String endpointURL = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=vera";
 
	public static void main(String[] args) throws Exception {
		URLConnection uc = new URL(endpointURL).openConnection();
		HttpURLConnection connection = (HttpURLConnection) uc;
		connection.setDoOutput(true);
		connection.setRequestMethod("GET");
		connection.connect();
		String line;
		InputStream inputStream = null;
		try {
			inputStream = connection.getInputStream();
		} catch (IOException e) {
			inputStream = connection.getErrorStream();
		}
		BufferedReader rd = new BufferedReader(new InputStreamReader(inputStream));
		while ((line = rd.readLine()) != null) {
			System.out.println(line);
		}
	}
}

To run this code you need to can get API key here.

So, have fun :)

PS Take a look at this too – Java OO wrapper for Google AJAX API.


You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

  • http://zayats.org.ua ai

    I’ve recently looked for API to some giant search engine.

    And stopped on Yahoo’s API. Reason is simple – they’re provide SOAP and REST access to their engine. More handy approach, eh?

  • http://www.vera.org.ua vira

    But Google is Google :-)

  • http:/mywebsearchengine.com harsha

    helloo.. could anybody help me..
    here in the above general code of google soap protocol.. i need google api jar file
    in the above link i am not able to download, there was an internal error with webserver..

    so could anybody forward to my mail account pleasee..

    harsha.siri@gmail.com

    thanking you forever…. harsha

  • http://www.vera.org.ua vira

    >>in the above link i am not able to download, there was an internal error with webserver..

    Sorry for inconvenience. This problem was fixed.

  • kulpster

    Vira,

    Can you explain the endpointURL string? I am trying to use my own google api key in the string but I am getting this back:

    {”responseData”: null, “responseDetails”: “invalid version”, “responseStatus”: 400}

    thanks.

  • http://www.vera.org.ua vira

    kulpster,

    Checked this example again and it doesn’t works anymore :( Try to use this url “http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=query_str” . It’s much simpler and works for me. It returns 4 result at a time, to get next 4 use “start” parameter.

    BTW, according to google docs api key is optional for now. If you provide key and some error occurs while searching Google can help work it out :)

  • Damian

    I get different results using Google.com and your code. Do you know the default preferences of ajax.googleapis.com?
    What I want to get is to obtain the same result with your code and google.com search.

  • http://aver.kiev.ua @ver

    2 Damian

    google ajax api and real google have different results (and mau be different index base).
    For example yandex.xml api and yandex.ru have different indexes (index yandex.xml more new).

    So, if you whant real google.com result – parse it ;-)

  • Damian

    But Google sends back 503 (Forbidden).
    I heard that I have to send Http-Agent header, but I don’t know how to send it. Do you?

  • http://www.vera.org.ua vira

    @Damian
    You just need to set corresponding system property
    e.g. System.setProperty(”http.agent”, “Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_5; ru-ru) AppleWebKit/525.27.1 (KHTML, like Gecko) Version/3.2.1 Safari/525.27.1″);

  • Damian

    Thank you :)

  • http://www.biochem.uwo.ca kevin

    How to parse?

    I want to get the same search number by using API as the real google search. Is there any java code example?

    2 Damian

    google ajax api and real google have different results (and mau be different index base).
    For example yandex.xml api and yandex.ru have different indexes (index yandex.xml more new).

    So, if you whant real google.com result – parse it ;-)

  • leo

    hi,

    can u pls tell me how to run the code for ajax api? i got a ajax api key as required..

  • http://www.vera.org.ua vira

    hi there!

    the code for ajax I provided here is very basic: it just prints Google response to console and query is statically defined :( . I’d recommend you to use my wrapper located here. In that post I placed an example of using Googler class. Currently, it doesn’t support API key, but this functionality could be easily added – just specify API key as value of “key” HTTP request parameter.

    BTW I’m going to add this some other functionality to the Googler in the distant future.