0

Can I perform a Google search using Java code? For example, if I search for "New York," will it send me the data stating all the information in a structured manner, such as JSON or XML data? Could you please share the code and or give me any API which does this?

4
  • 6
    You can use the Google API. Commented Oct 21, 2016 at 16:21
  • But I couldn't find any suitable API. custom search API is for a particular site. If you have something in mind could you please share? Commented Oct 21, 2016 at 16:25
  • 1
    stackoverflow.com/questions/19168929/… Commented Oct 21, 2016 at 16:50
  • Thanks. I have used developers.google.com/knowledge-graph for this purpose. Commented Oct 21, 2016 at 23:53

1 Answer 1

3

A sample code for google knowledge search.

Generate API using https://console.developers.google.com/apis/dashboard > credentials > Create credentials > API key

import java.util.List;

import org.json.simple.parser.JSONParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.HttpResponse;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;

@Service
public class GoogleSearchService {


    @Async
    public void searchGoogle(List<String> keywords){
        try {
            ObjectMapper mapper = new ObjectMapper();
            HttpTransport httpTransport = new NetHttpTransport();
            HttpRequestFactory requestFactory = httpTransport.createRequestFactory();
            JSONParser parser = new JSONParser();
            GenericUrl url = new GenericUrl("https://kgsearch.googleapis.com/v1/entities:search");
            url.put("query", "Kolkata");
            url.put("limit", "1");
            url.put("indent", "true");
            url.put("key", "xxxxxx");
            HttpRequest request = requestFactory.buildGetRequest(url);
            HttpResponse httpResponse = request.execute();
            String responseString = httpResponse.parseAsString();

            JsonNode node = mapper.readTree(responseString).get("itemListElement").get(0).get("result");
            System.out.println(node.get("name"));
            System.out.println(node.get("@type"));
            System.out.println(node.get("detailedDescription").get("articleBody"));

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

}
Sign up to request clarification or add additional context in comments.

4 Comments

Are you using "Custom Search API" for this?? Can you tell me which api are you using? in Custom Search API i need to configure custom search engine via adding domains, which can be a hassel because seach can be performed on any topic. if there is other than custom search api, let me know.
I have used google client API to establish HTTP connection to the service, run the query and get the response.
can you elaborate more on this api? i cant find it on google cloud console. is it same as custom search?
To do Search You have use Google's client API. This code snippet will prepare the Http request and call the Google search service. Use below URL ,knowledge search console.developers.google.com/apis/library/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.