我正在尝试使用java client for the Google custom search api ,但在Web上找不到任何示例教程。有人能为我提供一个简单的例子吗?谢谢!java google自定义搜索api
6
A
回答
2
以下示例基于1-1.30 client lib。由于没有太多的文件,这绝对不是最好的例子。事实上,我故意使用已弃用的方法来设置API密钥,因为更新的方式看起来过于复杂。
假设你已经包含在项目的构建路径正确的罐子依赖,一个基本的例子是:
//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
你需要得到一个自定义搜索引擎ID,并从Google API Console API密钥
4
我想在这里进行更正。
customsearch.setKey("YOUR_API_KEY_GOES_HERE");
不为客户工作,LIB 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();
}
1
这里是如何创建一个谷歌自定义搜索引擎,并在Java程序中使用一个简单的演示http://preciselyconcise.com/apis_and_installations/search_google_programmatically.php
+3
您的链接不再有效。 –
0
试试Google REST/JSON API:see API Guide。使用它很容易,只要你有你的引擎ID和钥匙。您只需使用您选择的库正确构建URL并使用响应JSON解析搜索结果即可。
相关问题
- 1. Google自定义搜索
- 2. Google自定义搜索API搜索图像
- 3. 如何解析Java中的Google自定义搜索API响应?
- 4. Google自定义搜索API的Java实现
- 5. 带自定义搜索框的Google自定义搜索?
- 6. Google自定义搜索的Google附加链接搜索框
- 7. 带有自定义搜索框和按钮的Google自定义搜索?
- 8. c#程序可以通过Google调用Google+ API和自定义搜索API
- 9. 如何从Google自定义搜索API创建JSON对象?
- 10. 使用Python的Google API自定义搜索 - 编程搜索结果
- 11. 通过Greasemonkey实现Google自定义搜索API
- 12. Google自定义搜索json api优化标签
- 13. 用于拼写检查的Google自定义搜索API
- 14. UrlFetchApp请求失败 - Google自定义搜索引擎API
- 15. 在.NET中使用Google自定义搜索API的步骤
- 16. 从Google自定义搜索API v1迁移到v2?
- 17. Angular2中的Google自定义搜索元素自定义
- 18. Google自定义搜索引擎的自定义CSS
- 19. Google+ api搜索
- 20. Android和谷歌自定义搜索API
- 21. 谷歌自定义搜索API javascript
- 22. Google自定义搜索搜索框未显示
- 23. Google自定义搜索 - 在查询字符串上搜索
- 24. Google的自定义搜索附加链接搜索框
- 25. Google自定义搜索引擎热门搜索部件
- 26. 如何从Google自定义搜索中搜索整个网络?
- 27. 使用谷歌自定义搜索API的Java代码
- 28. 谷歌自定义搜索api复杂搜索条件
- 29. 谷歌自定义搜索API - 反向图像搜索
- 30. 自定义谷歌自定义搜索
“API_KEY”和“CUSTOM_SEARCH_ENGINES”的值是什么?谢谢 –
对于搜索引擎ID - setCx()函数 - 转到https://www.google.com/cse/all,创建并选择一个自定义搜索引擎,然后单击“搜索引擎ID”按钮。 – Tom