1. 업종별 상가정보

import datetime as dt
import pandas as pd
import requests
from openAPI.public_config import *

numOfRows = "1000"  # 최대 1000
divId = "indsLclsCd"  # 대분류는 indsLclsCd, 중분류는 indsMclsCd, 소분류는 indsSclsCd를 사용
key = "Q"  # 대분류는 대분류코드값, 중분류는 중분류코드값, 소분류는 소분류코드값을 사용

url = 'http://apis.data.go.kr/B553077/api/open/sdsc2/storeListInUpjong'


df= pd.DataFrame()
title=[]

''' 
### while 문으로 사용
start_page = 755
pageNo = start_page

while True:
    params = {'serviceKey': access_key_De, 'pageNo': pageNo, 'numOfRows': numOfRows, 'divId': divId, 'key': key, 'type': 'json'}
    response = requests.get(url, params=params)
    raw_data=response.json()
    title=raw_data['header']['columns']
    if raw_data['body']['items'] == []:
        ct = dt.datetime.now().strftime
        print(f"{ct('%Y-%m-%d %H:%M:%S')} No Data in {pageNo}Page")
        break
    else:
        ct = dt.datetime.now().strftime
        data_temp = raw_data['body']['items']
        df_temp = pd.DataFrame(data_temp)
        df = pd.concat([df, df_temp], ignore_index = True)
        print(f"{ct('%Y-%m-%d %H:%M:%S')} Url Request Success, PageNo is {pageNo}")
        pageNo += 1
'''
### for 문으로 사용 (start
start_page = 755
end_page = 765
for pageNo in range(start_page,end_page):
    params = {'serviceKey': access_key_De, 'pageNo': pageNo, 'numOfRows': numOfRows, 'divId': divId, 'key': key, 'type': 'json'}
    response = requests.get(url, params=params)
    raw_data=response.json()
    title=raw_data['header']['columns']

    if raw_data['body']['items'] == []:
        ct = dt.datetime.now().strftime
        print(f"{ct('%Y-%m-%d %H:%M:%S')} No Data in {pageNo}Page")
        break
    else:
        ct = dt.datetime.now().strftime
        data_temp = raw_data['body']['items']
        df_temp = pd.DataFrame(data_temp)
        df = pd.concat([df, df_temp], ignore_index = True)
        print(f"{ct('%Y-%m-%d %H:%M:%S')} Url Request Success, PageNo is {pageNo}")
if len(df) != 0:
    ct = dt.datetime.now().strftime
    df.columns=title
    df.to_excel(f"data/{ct('%Y%m%d_%Hh%Mm')}_Q코드 소상공인 리스트.xlsx")

+ Recent posts