728x90
공공데이터포털
https://www.data.go.kr/index.do
import requests
import pandas as pd
import xml.etree.ElementTree as ET
def xmltodf(url, serviceKey, pageNo = 1, numOfRows = 100):
headers = {
"User-Agent": (
"Mozilla/5.0 (Windows NT 10.0;Win64; x64)\
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98\
Safari/537.36"
),
}
# 공공데이터api에서 요청주소
target = url +f"?serviceKey={serviceKey}&pageNo={pageNo}&numOfRows={numOfRows}"
# api요청
res = requests.get(target, headers=headers)
root = ET.fromstring(res.text)
# 동적으로 할당되는 변수들의 명칭을 담는 리스트
cols = []
# 동적 변수 할당
for item in root.iter('item'):
for child in item:
a = globals()[f'{child.tag}'] = []
cols.append(f'{child.tag}')
for item in root.iter('item'):
for child in item:
globals()[f'{child.tag}'].append(child.text)
df = pd.DataFrame()
for j in range(0, len(cols)):
df[f'{cols[j]}'] = globals()[f'{cols[j]}']
return df728x90
'개발공부 > 생성형 AI 기반 개발자 과정' 카테고리의 다른 글
| 주소록 관리 (0) | 2025.04.28 |
|---|---|
| 크롤링 활용(selenium 활용) (0) | 2025.04.17 |
| 크롤링 기초(api 활용편 - xml) (0) | 2025.04.16 |
| pandas - 7 (0) | 2025.04.06 |
| pandas - 6 (0) | 2025.04.06 |