Search code examples
javaapigoogle-search

Getting links of google search results


How can I search on google and then get the links of results(programmatically)? And please give me sample of source code. Thank you so much!


Solution

  • You can use the Google REST API, as described here: https://developers.google.com/custom-search/v1/using_rest#WorkingResults

    The result can be in JSON format, which you can parse to get the links.

    This is an example request:

    GET https://www.googleapis.com/customsearch/v1?key=INSERT-YOUR-KEY&cx=013036536707430787589:_pqjad5hr1a&q=flowers&alt=json
    

    Now you get a JSON as described. You can parse the JSON either with a JSON library, such as Jackson (recommended!), or you just "grep" through it using a regular expression:

        BufferedReader in = new BufferedReader(new StringReader(resultJson));
    
        Pattern regex = Pattern.compile(".*\"link\": \"(.*)\",");
        Collection<String> links = new ArrayList<String>();
        String line = null;
        while ((line = in.readLine()) != null) {
            Matcher matcher = regex.matcher(line);
            if (matcher.matches()) {
                String link = matcher.group(1);
                links.add(link);
            }
        }