2011-04-02 110 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); 

     } 
    }