【问题标题】:Google search using custom search使用自定义搜索的 Google 搜索
【发布时间】:2013-06-18 13:50:25
【问题描述】:

我被要求写一个倒排索引,所以我想作为一个开始写一个java程序,谷歌搜索一个单词并将结果放入一个数组列表中。

这是我的代码:

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!");

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

我需要在代码中进行哪些更改?也许我需要一种完全不同的方法?

【问题讨论】:

  • 关于在 Java 程序中从何处搜索的任何其他建议,@AndrewThompson?
  • 我怀疑您最终希望(或应该)使用爬虫来爬取您的网页并建立索引,而不是进行谷歌搜索。一个支持编写爬虫的库是 Crawler4J。此外,仅仅复制谷歌倒排索引似乎并不是一个好方法。对于测试,您可以从不使用爬虫索引单个网页开始。

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


【解决方案1】:

如果你想在你的应用中使用谷歌搜索,你应该使用谷歌的 API:

Custom search API

您会得到 JSON 格式的搜索结果。

【讨论】:

  • “JSON 格式”是什么意思?有没有办法在 Java 中使用它? (例如,将生成的 url 收集到一个字符串列表中?)
  • +1 - 我删除了我之前的评论,因为这个答案表明它是噪音。
  • @user1067083 JSON 是一种数据格式。在 Java 中,您可以轻松地使用它,例如使用 GSON 库。
  • @user1067083 抱歉,但从未使用过该 API。在这里您可以找到有关结果的更多信息:developers.google.com/custom-search/v1/…
猜你喜欢
  • 2019-07-17
  • 2013-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多