Java 使用自定义搜索的谷歌搜索

Java 使用自定义搜索的谷歌搜索,java,google-search,google-search-api,inverted-index,Java,Google Search,Google Search Api,Inverted Index,我被要求写一个倒排索引,所以我想开始写一个java程序,google搜索一个单词并将结果放入arraylist 这是我的密码: String search = "Dan"; String google = "http://www.google.com/cse/publicurl?cx=012216303403008813404:kcqyeryhhm8&q=" + search; URL url = new URL(google); HttpURLConnection conn = (Ht

我被要求写一个倒排索引,所以我想开始写一个java程序,google搜索一个单词并将结果放入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!");
String search=“Dan”;
字符串google=”http://www.google.com/cse/publicurl?cx=012216303403008813404:kcqyeryhhm8&q=“+搜索;
URL=新URL(谷歌);
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
conn.setRequestMethod(“GET”);
conn.setRequestProperty(“接受”、“应用程序/json”);
BufferedReader reader=新的BufferedReader(新的InputStreamReader(
(conn.getInputStream());
//将结果收集到字符串数组中
List resultsList=new ArrayList();
字符串r;
而((r=reader.readLine())!=null)
结果列表。添加(r);
连接断开();
System.out.println(“谷歌搜索:“+Search+”完成了!”);

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


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

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


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

关于在java程序中从何处搜索的任何其他建议,@AndrewThompson?我怀疑您最终希望(或应该)使用爬虫来爬网您的网页并建立索引,而不是使用谷歌搜索。一个支持编写爬虫程序的库是Crawler4J。而且,仅仅复制谷歌反向索引似乎不是一个好方法。为了进行测试,您可以在不使用爬虫的情况下为单个网页编制索引。这是什么意思“JSON格式”?有没有办法在Java中使用它?(例如,将结果url收集到字符串列表?+1-我删除了我之前的评论,因为这个答案显示它是噪音。@user1067083可能是这样的helps@user1067083JSON是一种数据格式。在Java中,您可以轻松地使用它,例如使用GSON库。@user1067083抱歉,但从未使用过该API。在这里,您可以找到有关结果的更多信息: