java google custom search api

青春壹個敷衍的年華 提交于 2019-12-04 09:35:13

问题


I'm trying to use the java client for the Google custom search api but couldn't find any sample tutorials on the web. Can someone provide a simple example for me to get started? Thank you!


回答1:


I want to make a correction here.

customsearch.setKey("YOUR_API_KEY_GOES_HERE");

does not work for client lib 1.6 but following does work

     Customsearch customsearch = new Customsearch(new NetHttpTransport(), new JacksonFactory());

    try {
        com.google.api.services.customsearch.Customsearch.Cse.List list = customsearch.cse().list("YOUR_SEARCH_STRING_GOES_HERE");
        list.setKey("YOUR_API_KEY_GOES_HERE");
        list.setCx("YOUR_CUSTOM_SEARCH_ENGINE_ID_GOES_HERE");
        Search results = list.execute();
        List<Result> items = results.getItems();

        for(Result result:items)
        {
            System.out.println("Title:"+result.getHtmlTitle());

        }

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }



回答2:


The following example is based on the 1-1.30 client lib. As there isn't much documentation this is definitely not the best example. In fact I'm intentionally using a deprecated method to set the API key as the newer way seemed overly complex.

Assuming you have included the correct jar dependencies in your project's build path, a basic example would be:

//Instantiate a Customsearch object with a transport mechanism and json parser    
Customsearch customsearch = new Customsearch(new NetHttpTransport(), new JacksonFactory());
//using deprecated setKey method on customsearch to set your API Key
customsearch.setKey("YOUR_API_KEY_GOES_HERE");
//instantiate a Customsearch.Cse.List object with your search string
com.google.api.services.customsearch.Customsearch.Cse.List list = customsearch.cse().list("YOUR_SEARCH_STRING_GOES_HERE");
//set your custom search engine id
list.setCx("YOUR_CUSTOM_SEARCH_ENGINE_ID_GOES_HERE")
//execute method returns a com.google.api.services.customsearch.model.Search object
Search results = list.execute();
//getItems() is a list of com.google.api.services.customsearch.model.Result objects which have the items you want
List<Result> items = results.getItems();
//now go do something with your list of Result objects

You'll need to get a custom search engine id, and an API key from the Google API Console




回答3:


Here is a simple demo on how to create a google custom search engine and use it from a java program http://preciselyconcise.com/apis_and_installations/search_google_programmatically.php




回答4:


Try Google REST / JSON api: see API Guide. It is very easy to work with it, as long as you have your engine id and key. All you have to do is properly construct the URL and parse the search results out of response JSON using a library of your choice.



来源:https://stackoverflow.com/questions/6917861/java-google-custom-search-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!