2016-10-21 10 views
0

Javaコードを使用してGoogle検索を実行できますか?たとえば、「ニューヨーク」を検索すると、という構造のすべての情報(JSONまたはXMLデータなど)がの形式で送信されますか?あなたはコードを共有してください、または私にこれを行うAPIを教えてください。Google検索をプログラムで使用する

+6

キーAPI>資格を作成する>https://console.developers.google.com/apis/dashboard>資格情報を使用して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ナレッジ検索のサンプルコードです。

あなたは、GoogleのAPIを使用することができます

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

} 
関連する問題