1. 네이터 광고 API에 사용할 key 생성하기¶
1.1 생성된 key를 변수에 저장¶
In [1]:
BASE_URL = 'https://api.naver.com'
CUSTOMER_ID = '(1)'
API_KEY = '(2)'
SECRET_KEY = '(3)'
2. header 함수 생성¶
In [2]:
import hashlib, hmac, base64, requests, time
def generate(timestamp, method, uri, secret_key):
message = "{}.{}.{}".format(timestamp, method, uri)
# hash = hmac.new(bytes(secret_key, "utf-8"), bytes(message, "utf-8"), hashlib.sha256)
hash = hmac.new(secret_key.encode("utf-8"), message.encode("utf-8"), hashlib.sha256)
hash.hexdigest()
return base64.b64encode(hash.digest())
def get_header(method, uri, api_key, secret_key, customer_id):
timestamp = str(int(time.time() * 1000))
signature = generate(timestamp, method, uri, SECRET_KEY)
return {'Content-Type': 'application/json; charset=UTF-8', 'X-Timestamp': timestamp, 'X-API-KEY': API_KEY, 'X-Customer': str(CUSTOMER_ID), 'X-Signature': signature}
3. 필요한 api 구현 ( 연관검색어 )¶
In [6]:
def call_RelKwdStat(_kwds_string):
dic_return_kwd = {}
uri = '/keywordstool'
method = 'GET'
prm = {'hintKeywords' : _kwds_string , 'showDetail':1}
# ManageCustomerLink Usage Sample
r = requests.get(BASE_URL + uri, params=prm, headers=get_header(method, uri, API_KEY, SECRET_KEY, CUSTOMER_ID))
r_data = r.json()
return r_data
In [7]:
# return된 결과 길이 확인
len(call_RelKwdStat('원피스')['keywordList'])
Out[7]:
In [8]:
# 최대 5개의 키워드 입력 가능
kwd_list = ['나이키','원피스', '운동화']
kwds_string = ','.join(kwd_list)
returnData = call_RelKwdStat(kwds_string)
In [10]:
# 상위 키워드 확인
import pandas as pd
df = pd.DataFrame(returnData['keywordList'])
df.sort_values(by='monthlyAveMobileCtr', ascending=False)[:10]
Out[10]:
댓글 없음:
댓글 쓰기