2014-02-20 31 views
0

我正在研究使用Java自動化Google搜索的解決方案。我研究過Google Custom Search API,但似乎沒有滿足要求。它顯示自定義搜索要求提前指定域,這對我不起作用,因爲我不知道這些域。我們希望進行Google網絡搜索,就像您從瀏覽器中進行搜索一樣。 Google Custom Search API有可能嗎?如果沒有人知道任何api /庫,最好在Java中工作?Google使用Java搜索

回答

2
public static void main(String[] args) { 
    String googleAJAX = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q="; 
    String searchFor = "boobies"; 

    /** 
     **Edit:** of course you can also get user input as a string 
     and search for that instead. i.e.: 
     String searchFor = (new Scanner(System.in)).nextLine(); 
    **/   

    URL url = new URL(googleAJAX + URLEncoder.encode(searchFor, "UTF-8")); 
    Reader read = new InputStreamReader(url.openStream(), "UTF-8"); 
    GoogleResults results = new Gson().fromJson(read, GoogleResults.class); 

    // Return results (title and URL) 
    System.out.println(results.getResponseData().getResults().get(0).getTitle()); 
    System.out.println(results.getResponseData().getResults().get(0).getUrl()); 
} 
+1

哇,看起來比我想象的要簡單得多。我會試試這個。哦,我喜歡searchFor的價值:) – greyfox

0

Selenium - 它的便攜性,可擴展性和非常簡單,快速入門。

它有用於Java和JavaScript的庫,無論你喜歡什麼。您可以快速自動執行用戶的操作。例如,轉到www.google.com並傳入查詢字符串。然後你可以使用XPATH等(許多其他方法)解析元素對象來檢索搜索結果。

您還可以使用xpath獲得googles autocomplete結果。

這不是使用谷歌搜索API,而是一個自定義的Web自動化工具。

+0

我聽說過硒,但從來沒有使用它。我也會研究這一點。 – greyfox