-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSON_DataFram
More file actions
37 lines (30 loc) · 1 KB
/
Copy pathJSON_DataFram
File metadata and controls
37 lines (30 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import urllib.parse
import urllib.request
import json
import pandas as pd
# 네이버 개발자센터에서 발급받은 ID/SECRET
client_id = "AR1qEhVIYM6TRV0bdcBM"
client_secret = "2rdVEC1idb"
query = urllib.parse.quote("ChatGPT")
url = f"https://openapi.naver.com/v1/search/news.json?query={query}&display=5"
# request 생성
request = urllib.request.Request(url)
request.add_header("X-Naver-Client-Id", client_id)
request.add_header("X-Naver-Client-Secret", client_secret)
# API 호출
response = urllib.request.urlopen(request)
rescode = response.getcode()
if rescode == 200:
news_json1 = response.read().decode('utf-8')
print("원본 JSON:\n", news_json1)
# JSON 파싱
json_result = json.loads(news_json1)
# Pandas DataFrame 변환
if 'items' in json_result:
df = pd.json_normalize(json_result['items'])
print("\nDataFrame 결과:")
print(df)
else:
print("JSON 응답에 'items' 키가 없습니다.")
else:
print("Error Code:" + str(rescode))