Search Trendのユースケース
    • PDF

    Search Trendのユースケース

    • PDF

    記事の要約

    Classic/VPC環境で利用できます。

    Search Trend APIを使用するユースケースを紹介します。

    Search Trend

    NAVER統合検索の期間別トレンドデータ統計を年齢、性別、検索環境別に分析する Search Trend APIのユースケースを説明します。

    Java

    Javaベースの Search Trend APIのサンプルコードは次の通りです。

    import java.io.BufferedReader;
    import java.io.DataOutputStream;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.URL;
    
    public class APIExamDatalabTrend {
    
        public static void main(String[] args) {
            String clientId = "YOUR_CLIENT_ID";
            String clientSecret = "YOUR_CLIENT_SECRET";
    
            try {
                String apiURL = "https://naveropenapi.apigw.ntruss.com/datalab/v1/search";
                String body = "{\"startDate\":\"2017-01-01\",\"endDate\":\"2017-04-30\",\"timeUnit\":\"month\",\"keywordGroups\":[{\"groupName\":\"ハングル\",\"keywords\":[\"ハングル\",\"korean\"]},{\"groupName\":\"英語\",\"keywords\":[\"英語\",\"english\"]}],\"device\":\"pc\",\"ages\":[\"1\",\"2\"],\"gender\":\"f\"}";
                URL url = new URL(apiURL);
                HttpURLConnection con = (HttpURLConnection)url.openConnection();
                con.setRequestMethod("POST");
                con.setRequestProperty("X-NCP-APIGW-API-KEY-ID", clientId);
                con.setRequestProperty("X-NCP-APIGW-API-KEY", clientSecret);
                con.setRequestProperty("Content-Type", "application/json");
    
                con.setDoOutput(true);
                DataOutputStream wr = new DataOutputStream(con.getOutputStream());
                wr.write(body.getBytes());
                wr.flush();
                wr.close();
    
                int responseCode = con.getResponseCode();
                BufferedReader br;
                if(responseCode==200) { 
                    br = new BufferedReader(new InputStreamReader(con.getInputStream()));
                } else {  
                    br = new BufferedReader(new InputStreamReader(con.getErrorStream()));
                }
    
                String inputLine;
                StringBuffer response = new StringBuffer();
                while ((inputLine = br.readLine()) != null) {
                    response.append(inputLine);
                }
                br.close();
                System.out.println(response.toString());
    
            } catch (Exception e) {
                System.out.println(e);
            }
        }
    }
    

    Python

    Pythonベースの Search Trend APIのサンプルコードは次の通りです。

    import os
    import sys
    import urllib.request
    client_id = "YOUR_CLIENT_ID"
    client_secret = "YOUR_CLIENT_SECRET"
    url = "https://naveropenapi.apigw.ntruss.com/datalab/v1/search";
    body = "{\"startDate\":\"2017-01-01\",\"endDate\":\"2017-04-30\",\"timeUnit\":\"month\",\"keywordGroups\":[{\"groupName\":\"ハングル\",\"keywords\":[\"ハングル\",\"korean\"]},{\"groupName\":\"英語\",\"keywords\":[\"英語\",\"english\"]}],\"device\":\"pc\",\"ages\":[\"1\",\"2\"],\"gender\":\"f\"}";
    
    request = urllib.request.Request(url)
    request.add_header("X-NCP-APIGW-API-KEY-ID",client_id)
    request.add_header("X-NCP-APIGW-API-KEY",client_secret)
    request.add_header("Content-Type","application/json")
    response = urllib.request.urlopen(request, data=body.encode("utf-8"))
    rescode = response.getcode()
    if(rescode==200):
        response_body = response.read()
        print(response_body.decode('utf-8'))
    else:
        print("Error Code:" + rescode)
    

    PHP

    PHPベースの Search Trend APIのサンプルコードは次の通りです。

    <?php
    
    $client_id = "YOUR_CLIENT_ID";
    $client_secret = "YOUR_CLIENT_SECRET";
    
    $url = "https://naveropenapi.apigw.ntruss.com/datalab/v1/search";
    $body = "{\"startDate\":\"2017-01-01\",\"endDate\":\"2017-04-30\",\"timeUnit\":\"month\",\"keywordGroups\":[{\"groupName\":\"ハングル\",\"keywords\":[\"ハングル\",\"korean\"]},{\"groupName\":\"英語\",\"keywords\":[\"英語\",\"english\"]}],\"device\":\"pc\",\"ages\":[\"1\",\"2\"],\"gender\":\"f\"}";
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $headers = array();
    $headers[] = "X-NCP-APIGW-API-KEY-ID: ".$client_id;
    $headers[] = "X-NCP-APIGW-API-KEY: ".$client_secret;
    $headers[] = "Content-Type: application/json";
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    
    curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
    
    $response = curl_exec ($ch);
    $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    echo "status_code:".$status_code."
    ";
    curl_close ($ch);
    if($status_code == 200) {
        echo $response;
    } else {
        echo "エラー内容: ".$response;
    }
    ?>
    

    JavaScript

    JavaScriptベースの Search Trend APIのサンプルコードは次の通りです。

    var request = require('request');
    
    var client_id = 'YOUR_CLIENT_ID';
    var client_secret = 'YOUR_CLIENT_SECRET';
    
    var api_url = 'https://naveropenapi.apigw.ntruss.com/datalab/v1/search';
    var request_body = {
      startDate: '2017-01-01',
      endDate: '2017-04-30',
      timeUnit: 'month',
      keywordGroups: [
        {
          groupName: 'ハングル',
          keywords: ['ハングル', 'korean'],
        },
        {
          groupName: '英語',
          keywords: ['英語', 'english'],
        },
      ],
      device: 'pc',
      ages: ['1', '2'],
      gender: 'f',
    };
    
    request.post(
      {
        url: api_url,
        body: JSON.stringify(request_body),
        headers: {
          'X-NCP-APIGW-API-KEY-ID': client_id,
          'X-NCP-APIGW-API-KEY': client_secret,
          'Content-Type': 'application/json',
        },
      },
      function(error, response, body) {
        console.log(response.statusCode);
        console.log(body);
      },
    );
    

    C#

    C# ベースの Search Trend APIのサンプルコードは次の通りです。

    using System;
    using System.Net;
    using System.Text;
    using System.IO;
    
    namespace NaverAPI_Guide
    {
        public class APIExamDatalabTrend
        {
            static void Main(string[] args)
            {
                string url = "https://naveropenapi.apigw.ntruss.com/datalab/v1/search";
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Headers.Add("X-NCP-APIGW-API-KEY-ID", "YOUR-CLIENT-ID");
                request.Headers.Add("X-NCP-APIGW-API-KEY", "YOUR-CLIENT-SECRET");
                request.ContentType = "application/json";
                request.Method = "POST";
                string body = "{\"startDate\":\"2017-01-01\",\"endDate\":\"2017-04-30\",\"timeUnit\":\"month\",\"keywordGroups\":[{\"groupName\":\"ハングル\",\"keywords\":[\"ハングル\",\"korean\"]},{\"groupName\":\"英語\",\"keywords\":[\"英語\",\"english\"]}],\"device\":\"pc\",\"ages\":[\"1\",\"2\"],\"gender\":\"f\"}";
                byte[] byteDataParams = Encoding.UTF8.GetBytes(body);
                request.ContentLength = byteDataParams.Length;
                Stream st = request.GetRequestStream();
                st.Write(byteDataParams, 0, byteDataParams.Length);
                st.Close();
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream stream = response.GetResponseStream();
                StreamReader reader = new StreamReader(stream, Encoding.UTF8);
                string text = reader.ReadToEnd();
                stream.Close();
                response.Close();
                reader.Close();
                Console.WriteLine(text);
            }
        }
    }
    

    この記事は役に立ちましたか?

    Changing your password will log you out immediately. Use the new password to log back in.
    First name must have atleast 2 characters. Numbers and special characters are not allowed.
    Last name must have atleast 1 characters. Numbers and special characters are not allowed.
    Enter a valid email
    Enter a valid password
    Your profile has been successfully updated.