python_apps/crwlers/conteenew/nhotkwd.py
2023-11-03 14:49:12 +09:00

49 lines
1.6 KiB
Python

# 20210711
# Naver Datalab 쇼핑인사이트 카테고리별 상위 키워드 추출
# POST로 파라미터를 넣어서 결과값을 받아옵니다.
import requests, urllib, sys, json
input = {
"cid": str(sys.argv[1]), #호출발식 : python3 $PATH/nhotkwd.py {카테고리코드}
"timeUnit": "date",
"startDate": "2021-06-01",
"endDate": "2021-07-10",
"age": "",
"gender": "",
"page": "1",
"count": "200"
}
query = urllib.parse.quote_plus(input["cid"])
url = "https://datalab.naver.com/shoppingInsight/getCategoryKeywordRank.naver"
headers = { #네이버 서버 속임용 레퍼러 조작
'Host': 'datalab.naver.com',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:89.0) Gecko/20100101 Firefox/89.0',
'Accept': '*/*',
'Accept-Language': 'ko-KR,ko;q=0.8,en-US;q=0.5,en;q=0.3',
'Accept-Encoding': 'gzip, deflate, br',
'Referer': 'https://datalab.naver.com/shoppingInsight/sCategory.naver',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'X-Requested-With': 'XMLHttpRequest',
'Origin': 'https://datalab.naver.com',
'Connection': 'keep-alive',
'TE': 'Trailers',
'Pragma': 'no-cache',
'Cache-Control': 'no-cache'
}
resp = requests.post(url, data = input, headers = headers)
resp.raise_for_status()
resp.encoding='UTF-8'
data = json.loads(resp.content)
for i in range(len(data['ranks'])):
cid = str(input["cid"])
rank = str(data['ranks'][i]['rank'])
kwd = str(data['ranks'][i]['keyword'])
print(cid + "\t" + rank + "\t" + kwd)
# 결과값 포멧은 네이버카테고리코드 랭크 키워드 순임.