java 谷歌自定义搜索API

发布于 2024-11-27 17:07:21 字数 193 浏览 2 评论 0 原文

我正在尝试使用 Java 客户端Google 自定义搜索 API 但在网上找不到任何示例教程。有人可以提供一个简单的例子让我开始吗?谢谢你!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

江心雾 2024-12-04 17:07:22

以下示例基于 1- 1.30 客户端库。由于没有太多文档,这绝对不是最好的例子。事实上,我故意使用一种已弃用的方法来设置 API 密钥,因为较新的方法似乎过于复杂。

假设您已在项目的构建路径中包含正确的 jar 依赖项,一个基本示例是:

//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

您需要从 Google API 控制台

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

仅此而已 2024-12-04 17:07:22

这是一个关于如何创建 Google 自定义搜索引擎并从 Java 程序使用它的简单演示 http:// Precisionconcise.com/apis_and_installations/search_google_programmatically.php

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

柠檬心 2024-12-04 17:07:22

尝试 Google REST / JSON api:请参阅 API 指南。只要您有引擎 ID 和密钥,使用它就非常容易。您所要做的就是正确构建 URL 并使用您选择的库从响应 JSON 中解析搜索结果。

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.

南渊 2024-12-04 17:07:21

我想在这里更正一下。

customsearch.setKey("YOUR_API_KEY_GOES_HERE");

不适用于客户端库 1.6,但以下内容确实有效

     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();
    }

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();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文