2016-10-21 4 views
0

Java 코드를 사용하여 Google 검색을 수행 할 수 있습니까? 예를 들어 "New York"을 검색하면 구조의 방식 (예 : JSON 또는 XML 데이터)의 모든 정보를 나타내는 데이터가 나에게 전송됩니까? 코드를 공유하고이 API를 제공 해줄 수 있습니까?Google 프로그래밍 방식으로 Java를 사용하여 검색

+6

당신은 구글 API를 사용하여 핵심 API를 API를 생성합니다. – Maroun

+0

하지만 적절한 API를 찾을 수 없습니다. 맞춤 검색 API는 특정 사이트에 대한 것입니다. 무언가를 염두에두면 공유 할 수 있습니까? – Debopam

+1

http://stackoverflow.com/questions/19168929/is-there-any-way-to-return-google-search-results-to-xml-or-json-without-being-a – j1nrg

답변

2

Google 지식 검색을위한 샘플 코드입니다.

> 자격 증명을 작성>https://console.developers.google.com/apis/dashboard> 자격 증명을 사용하여

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(); 
     } 
    } 

} 
관련 문제