谷歌搜索使用自定义搜索

时间:2013-06-18 13:50:25

标签: java google-search google-search-api inverted-index

我要求写一个倒排索引,所以我想首先编写一个java程序,谷歌搜索一个单词并将结果放入arraylist。

这是我的代码:

String search = "Dan";
String google = "http://www.google.com/cse/publicurl?cx=012216303403008813404:kcqyeryhhm8&q=" + search;
URL url = new URL(google);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
BufferedReader reader = new BufferedReader(new InputStreamReader(
    (conn.getInputStream())));
// Gather the results to a String array
List<String> resultsList = new ArrayList<String>();
String r;
while ((r = reader.readLine()) != null)
    resultsList.add(r);
conn.disconnect();
System.out.println("Google Search for: " + search + " Is Done!");

程序在中间没有崩溃的情况下运行,但我只得到一个页面的源代码(不包含任何链接)。

我需要在代码中更改什么?也许我需要一种完全不同的方法?

1 个答案:

答案 0 :(得分:2)

如果您想在自己的应用中使用Google搜索,则应使用Google的API:

Custom search API

您将获得JSON格式的搜索结果。

相关问题