[Python]공공데이터포털 활용

Jae Gyeong Lee·2023년 3월 2일
0

공공데이터 포털 활용

import requests; import pprint; import json #필요 모듈

url = '주소'

response = requests.get(url, verify=False)
contents = response.text

# 이쁘게 출력, indent = 들여쓰기
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(contents)

# 확인
json_ob = json.loads(contents)
print(json_ob)


# 이제 출력값 확인하고 원하는 값으로 접근

# 예
json_ob['getRecommendedKr']['item'][0]['MAIN_TITLE']
json_ob['getRecommendedKr']['item'][0]['PLACE']


# 반복 추출
cont = []; place = []

for n in range(201):   #불러온 값은 total 201
    aa = json_ob['getRecommendedKr']['item'][n]['MAIN_TITLE']
    bb = json_ob['getRecommendedKr']['item'][n]['PLACE']
    cont.append(aa)
    place.append(bb)
result = list(zip(cont, place))


# 저장
import pandas as pd
df = pd.DataFrame(result, columns = ['title', 'place'])
df.to_excel('data3.xlsx', index = False)

https://coding-potato.tistory.com/8

profile
안녕하세요 반갑습니다. 공부한 내용들을 기록하고 있습니다.

0개의 댓글