如何创建一个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