부동산 데이터는 국토부에서 가져올수가 있는데,
국토부 데이터는 하나씩 특정 조회를 통해서 가져올 수 있다.
내가 원하는 모든 데이터를 가져오기에는 매우 큰 노가다가 필요하다.
국토부 데이터 파싱하기
- 국토부에서 특정조건으로 데이터를 다운로드
- 아래와 같이 parsing이 가능하다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
f = open("../data/data.csv","r", encoding='cp949') | |
lines = f.readlines() | |
cols = lines[15][:-1] | |
cols = cols.split("\"") | |
cols = list(filter(lambda x: (x != '') & (x != ',') ,cols)) | |
data = lines[16:] | |
rows = [] | |
for d in data: | |
d = d[:-1].split("\"") | |
d = list(filter(lambda x: (x != '') & (x != ',') ,d)) | |
rows.append(d) | |
df = pd.DataFrame(rows, columns=cols) |
국토부 데이터 결과는 아래와 같다.
시군구 번지 본번 부번 단지명 전월세구분 전용면적(㎡) 계약년월 계약일 보증금(만원) 월세(만원) 층 건축년도 도로명
0 경기도 성남분당구 구미동 77 0077 0000 까치마을(1단지)(대우롯데선경) 전세 84.79 201905 2 53,000 0 10 1995 미금로 184
1 경기도 성남분당구 구미동 77 0077 0000 까치마을(1단지)(대우롯데선경) 전세 51.32 201905 3 29,000 0 1 1995 미금로 184
2 경기도 성남분당구 구미동 77 0077 0000 까치마을(1단지)(대우롯데선경) 월세 51.32 201905 6 12,000 60 4 1995 미금로 184
공공데이터에서 API를 통한 호출
- opendata는 API를 제공하고 있어
serviceKey
를 발급받고 아래와 같이 조회하면 된다. - 파라미터로 사용하는 값은 월데이터
ex: 201901
와법정동코드
이다. - 이때 하나의 팁은
numOfRows
의 값을 9999로 넘겨주면!? 한번의 요청으로 모든 결과를 가져올수 있다. - 이렇게 해야하는 이유는 요청 제한이 있기 때문이다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
url = "http://openapi.molit.go.kr/OpenAPI_ToolInstallPackage/service/rest/RTMSOBJSvc/getRTMSDataSvcAptTradeDev" | |
querystring = {"pageNo":"1","startPage":"1","numOfRows":"1","pageSize":"10","LAWD_CD":"41135","DEAL_YMD":"201905","type":"json", "serviceKey":"<service_key>"} | |
headers = { | |
'cache-control': "no-cache", | |
'postman-token': "e8d4c5d9-9287-549d-b5bc-9cdd60e76e1d" | |
} | |
response = requests.request("GET", url, headers=headers, params=querystring)## | |
import xml.etree.ElementTree as ET | |
root = ET.fromstring(response.content) | |
for child in root.find('body'): | |
print (child.tag, child.attrib, child.text) | |
item_list = [] | |
for child in root.find('body').find('items'): | |
elements = child.findall('*') | |
data = {} | |
for element in elements: | |
tag = element.tag.strip() | |
text = element.text.strip() | |
# print tag, text | |
data[tag] = text | |
item_list.append(data) | |
import pandas as pd | |
df = pd.DataFrame(item_list) |
공공데이터의 결과는 아래와 같다.
건축년도 년 법정동 보증금액 아파트 월 월세금액 일 전용면적 지번 지역코드 층
0 1992 2019 분당동 30,000 샛별마을(동성) 5 0 1 59.4 35 41135 5
1 1993 2019 분당동 35,000 장안타운(건영) 5 0 1 67.73 66 41135 9
2 1993 2019 분당동 26,500 장안타운(건영) 5 0 1 53.72 66 41135 1
'[연재코너1] 파이썬으로 부동산 데이터 분석 해봐요 > Self 부동산 데이터 분석' 카테고리의 다른 글
[부동산] (주의) 공공데이터에는 중복된 데이터가 있다. (0) | 2020.02.25 |
---|---|
[부동산] 공공데이터 층에 NaN? 0층? (0) | 2020.02.25 |
[부동산 데이터] 부동산 실거래가 다운로드와 파일 읽는 방법 (인코딩) (0) | 2019.12.11 |
[부동산데이터] 서울특별시 연도별 거래량이 감소하고 있다! (0) | 2019.12.09 |
서울에서 3년동안 가장 많은 아파트 실거래가 있었던 동은? 년도별 아파트 매매 건수 비교 (0) | 2019.12.07 |