开发者

Retrieve Google results programmatically

How do I create a 开发者_如何学CJava program that enters the words "Hello World" into Google and then retrieves the html from the results page? I'm not trying to use the Robot class.


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

If you make repeated programmatic requests to Google in this way they will start to redirect you to "we're sorry but you look like a robot" pages pretty quick.

What you may be better doing is using Google's custom search api.


For performing google search through a program, you will need a developer api key and a custom search engine id. You can get the developer api key and custom search engine id from below urls.

https://cloud.google.com/console/project'>Google Developers Console https://www.google.com/cse/all'>Google Custom Search

After you got the both the key and id use it in below program. Change apiKey and customSearchEngineKey with your keys.

For step by step information please visit - 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);

        }
    }

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜