如何創建一個Java程序,將單詞「Hello World」輸入到Google中,然後從結果頁面中檢索HTML?我沒有嘗試使用Robot類。以編程方式檢索Google結果
1
A
回答
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); } }
相關問題
- 1. 如何以編程方式訪問Google搜索結果
- 2. 以編程方式獲取Google搜索結果
- 3. 如何以編程方式檢索谷歌結果?
- 4. 以編程方式從Google+中檢索我的所有活動
- 5. 如何以編程方式檢索URL的總數Google + 1的
- 6. 有沒有辦法以編程方式訪問Google的搜索引擎結果?
- 7. 以編程方式檢索SPList URL
- 8. 以編程方式檢索「InitialRotationPreference」的值
- 9. 以R.編程方式檢索顏色
- 10. Android:以編程方式檢索layout_marginBottom?
- 11. 以編程方式檢索POJO對象?
- 12. 以編程方式檢索Bean
- 13. VM參數:正確檢索並以編程方式檢索
- 14. Google地方搜索結果
- 15. 以編程方式檢索所有運輸方式的列表
- 16. 編程方式檢索WYSIHTML5編輯
- 17. Magento搜索結果以我的方式
- 18. 以編程方式檢索sqlite中的索引
- 19. 以編程方式檢索方法的參數和值
- 20. 如何從styles.xml以編程方式檢索樣式屬性
- 21. 以編程方式創建Google Apps域
- 22. 以編程方式下載Google文檔?
- 23. 如何以編程方式查詢Google?
- 24. 如何以編程方式檢索ClickOnce更新程序?
- 25. 如何以編程方式查詢Google搜索量
- 26. 以編程方式獲取Google中索引頁面的數量?
- 27. 部分檢索Google Health API的結果
- 28. Google電子表格。檢索IMPORTDATA-結果
- 29. Google App Engine以編程方式檢查備份的成功
- 30. Google api搜索框結果編號
你爲什麼要這麼做?如果您只想獲得Google的搜索結果,那麼您最好將原始HTTP請求發送給Google服務器。不過要小心,因爲他們在執行服務條款方面很不錯。 – templatetypedef 2011-04-02 22:56:38
儘管使用網絡瀏覽器,java.awt.Robot(點擊,發送鍵和捕獲屏幕)和OCR算法是一個創造性的想法,但是;-) – 2011-04-03 01:59:19