Api Crawling /Json 형식 데이터 단일/다중 수집 서울 열린데이터 광장

2024. 2. 1. 17:26Data Science/Study 자료

JSON 형식 단일데이터 수집

 

URL=f'http://openapi.seoul.go.kr:8088/{SERVICE_KEY}/json/tbLnOpendataRtmsV/1/100/'
URL
import requests
import json
import pandas as pd

req = requests.get(URL)
content = req.json()   #json데이터 불러오기​

content 내용

result = pd.DataFrame(content['tbLnOpendataRtmsV']['row']) #중요부분
result

다중데이터 수집

공공데이터는 블러올떄 리미트가 걸린 경우들이 많음 서울 열린데이터 광장의 경우 1000개 제한

 

data = None
SERVICE_KEY ='***********************************'
for j in range(1,5):
    url = f'http://openapi.seoul.go.kr:8088/{SERVICE_KEY}/json/tbLnOpendataRtmsV/{1+((j-1)*1000)}/{j*1000}'
    # url = f'http://openapi.seoul.go.kr:8088/{service_key}/json/tbLnOpendataRentV/1/1000/2023/11560'
    print(url)
    
    #하나의 URL을 Json으로 받아 데이터 프레임으로 만듦
    req = requests.get(url)
    content = req.json()
    con = content['tbLnOpendataRtmsV']['row']
    result = pd.DataFrame(con)
    
    data = pd.concat([data, result])
    
data = data.reset_index(drop=True)
data.head()

for문과 f포멧팅을 이용하여 가져올 데이터수를 조정할수 있다