2011-04-02 411 views
1

如何创建一个Java程序,将单词“Hello World”输入到Google中,然后从结果页面中检索HTML?我没有尝试使用Robot类。以编程方式检索Google结果

+3

你为什么要这么做?如果您只想获得Google的搜索结果,那么您最好将原始HTTP请求发送给Google服务器。不过要小心,因为他们在执行服务条款方面很不错。 – templatetypedef 2011-04-02 22:56:38

+0

尽管使用网络浏览器,java.awt.Robot(点击,发送键和捕获屏幕)和OCR算法是一个创造性的想法,但是;-) – 2011-04-03 01:59:19

回答

7
URL url = new URL("http://www.google.com/search?q=hello+world"); 
url.openStream(); // returns an InputStream which you can read with e.g. a BufferedReader 

如果进行多次程序要求谷歌通过这种方式,他们将开始您重定向到“很抱歉,但你看起来像一个机器人”的页面相当快。

你可能会更好做的是使用谷歌的custom search api

+0

自定义搜索API是最适合您的目的。我无法通过@Finbarr所示的正常查询从google获得结果。你可以在这里看到一个使用自定义搜索引擎的教程http://preciselyconcise.com/apis_and_installations/search_google_programmatically.php – 2014-01-28 16:38:47

1

对于通过程序执行谷歌搜索,您将需要一个开发人员API密钥和自定义搜索引擎ID。您可以从下面的URL获取开发者API密钥和自定义搜索引擎ID。

https://cloud.google.com/console/project'>Google开发者控制台 https://www.google.com/cse/all'>Google自定义搜索

后你有两个密钥和id在下面的程序中使用它。使用您的密钥更改apiKey和customSearchEngineKey。

一步一步的信息,请访问 - http://www.basicsbehind.com/google-search-programmatically/

 

    import java.io.BufferedReader; 
    import java.io.InputStreamReader; 
    import java.net.HttpURLConnection; 
    import java.net.URL; 

    public class CustomGoogleSearch { 
     final static String apiKey = "AIzaSyAFmFdHiFK783aSsdbq3lWQDL7uOSbnD-QnCnGbY"; 
     final static String customSearchEngineKey = "00070362344324199532843:wkrTYvnft8ma"; 
     final static String searchURL = "https://www.googleapis.com/customsearch/v1?"; 

     public static String search(String pUrl) { 
      try { 
       URL url = new URL(pUrl); 
       HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
       BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream())); 

       String line; 
       StringBuffer buffer = new StringBuffer(); 
       while ((line = br.readLine()) != null) { 
        buffer.append(line); 
       } 
       return buffer.toString(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
      return null; 
     } 
     private static String buildSearchString(String searchString, int start, int numOfResults) { 
      String toSearch = searchURL + "key=" + apiKey + "&cx=" + customSearchEngineKey + "&q="; 

      // replace spaces in the search query with + 
      String newSearchString = searchString.replace(" ", "%20"); 

      toSearch += newSearchString; 

      // specify response format as json 
      toSearch += "&alt=json"; 

      // specify starting result number 
      toSearch += "&start=" + start; 

      // specify the number of results you need from the starting position 
      toSearch += "&num=" + numOfResults; 

      System.out.println("Seacrh URL: " + toSearch); 
      return toSearch; 
     } 


     public static void main(String[] args) throws Exception { 

      String url = buildSearchString("BasicsBehind", 1, 10); 
      String result = search(url); 
      System.out.println(result); 

     } 
    }