Search Trend 예제
- 인쇄
- PDF
Search Trend 예제
- 인쇄
- PDF
기사 요약
이 요약이 도움이 되었나요?
의견을 보내 주셔서 감사합니다.
Classic/VPC 환경에서 이용 가능합니다.
Search Trend API를 사용하는 예제를 소개합니다.
Search Trend
네이버 통합검색의 기간별 트렌드 데이터 통계를 연령, 성별, 검색 환경별로 분석하는 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 "Error 내용:".$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);
}
}
}
이 문서가 도움이 되었습니까?