<jupyter_start><jupyter_text>년도 월별 지역구 자료 뽑아오기.<jupyter_code>import pandas as pd
from bs4 import BeautifulSoup
import requests
import pandas as pd
import numpy as np
def get_data():
    code = {'종로구': '11110', '중구': '11140', '용산구': '11170', '성동구': '11200', 
            '광진구': '11215', '동대문구': '11230', '중랑구': '11260', '성북구': '11290', 
            '강북구': '11305', '도봉구': '11320', '노원구': '11350', '은평구': '11380', 
            '서대문구': '11410', '마포구': '11440', '양천구': '11470', '강서구': '11500',
            '구로구': '11530', '금천구': '11545', '영등포구': '11560', '동작구': '11590',
            '관악구': '11620', '서초구': '11650', '강남구': '11680', '송파구': '11710', '강동구': '11740'}

    dateList01 = ["201601","201602","201603","201604","201605","201606","201607","201608","201609","201610","201611","201612"]
    dateList02 = ["201701","201702","201703","201704","201705","201706","201707","201708","201709","201710","201711","201712"]
    dateList03 = ["201801","201802","201803","201804","201805","201806","201807","201808"]

    ## URL request --> 받아오기 ## --> 하루 1000트래픽 한정(1 계정당)

    url = 'http://openapi.molit.go.kr:8081/OpenAPI_ToolInstallPackage/service/rest/RTMSOBJSvc/getRTMSDataSvcAptTrade?'
    # 서비스키 중요 --> 공공데이터포털에서 오픈API로 받은 인증키를 여기에 입력
    serviceKey = 'serviceKey=' + "9rL%2Fv5RbvEC4bLji%2B3hTKF1wuBPkoVk6nnKg54x54CON<KEY>w%3D%3D" + "&"

    list = code.keys()
    list01=[]
    for i in list:
        list01.append(i)
 
    # num은 추출 년도에 맞게 수정하기.
    num = 201801
    for k in dateList03:
        data_list=[]
        for i in list01:
            LAWD_CD = 'LAWD_CD=' + code[i] + '&' # 법정 코드 번호 --> 가운데 숫자만 변화주면됨. (위 codedict)
            DEAL_YMD = 'DEAL_YMD=' + k # 기간 --> 수집시기는 우리의 몫
            url_all = url + serviceKey + LAWD_CD + DEAL_YMD
            res = requests.get(url_all)
            text = res.text
            soup = BeautifulSoup(text,'lxml-xml')

            for item in soup.select('item'):
                price = item.거래금액.text
                apt = item.아파트.text
                add = item.법정동.text
                howbig = item.전용면적.text
                zep = item.지번.text
                floor = item.층.text
                
                data_list.append([apt,add+zep,price,howbig,floor+"층"])
        data_pd=pd.DataFrame(data_list)  
        data_pd.to_csv('clean_'+str(num)+'.csv',encoding='cp949')
        num +=1

# get_data()<jupyter_output><empty_output><jupyter_text>전체 주소  뽑아 오기.<jupyter_code>import numpy as np
def get_code():
    code = {'종로구': '11110', '중구': '11140', '용산구': '11170', '성동구': '11200', 
            '광진구': '11215', '동대문구': '11230', '중랑구': '11260', '성북구': '11290', 
            '강북구': '11305', '도봉구': '11320', '노원구': '11350', '은평구': '11380', 
            '서대문구': '11410', '마포구': '11440', '양천구': '11470', '강서구': '11500',
            '구로구': '11530', '금천구': '11545', '영등포구': '11560', '동작구': '11590',
            '관악구': '11620', '서초구': '11650', '강남구': '11680', '송파구': '11710', '강동구': '11740'}

    dateList = ["201601","201602","201603","201604","201605","201606","201607","201608","201609","201610","201611","201612"
    "201701","201702","201703","201704","201705","201706","201707","201708","201709","201710","201711","201712",
    "201801","201802","201803","201804","201805","201806","201807","201808"]

    ## URL request --> 받아오기 ## --> 하루 1000트래픽 한정(1 계정당)

    url = 'http://openapi.molit.go.kr:8081/OpenAPI_ToolInstallPackage/service/rest/RTMSOBJSvc/getRTMSDataSvcAptTrade?'
    # 서비스키 중요 --> 공공데이터포털에서 오픈API로 받은 인증키를 여기에 입력
    serviceKey = 'serviceKey=' + "9rL%2Fv5RbvEC4bLji%2B3hTKF1wuBPkoVk6nnKg54x54CON8OOz5cLFmRFdIWf38wJmwxilBt4XYpuqm0jzpCoM8w%3D%3D" + "&"
   
    # code 키 추출
    list = code.keys()
    list01=[]
    for i in list:
        list01.append(i)


    tmp_list = []
    for k in dateList:      
        for i in list01:
            LAWD_CD = 'LAWD_CD=' + code[i] + '&' # 법정 코드 번호 --> 가운데 숫자만 변화주면됨. (위 codedict)
            DEAL_YMD = 'DEAL_YMD=' + k # 기간 --> 수집시기는 우리의 몫
            url_all = url + serviceKey + LAWD_CD + DEAL_YMD
            res = requests.get(url_all)
            text = res.text
            soup = BeautifulSoup(text,'lxml-xml')

            for item in soup.select('item'):                 
                add = item.법정동.text  
                zep = item.지번.text                
                tmp_list.append(['서울시', i , add + zep])    
        data_pd=pd.DataFrame(tmp_list)

    return data_pd.to_csv('house01_code.csv',encoding='cp949')

# get_code()<jupyter_output><empty_output><jupyter_text>뽑아온 주소 중복주소 제거하기.<jupyter_code>def clean():

    open_code= pd.read_csv('house01_code.csv',encoding="949")
    open_code['hap'] = open_code['0']+open_code['1']+open_code['2']
    np_code = np.array(open_code)
    clean_code = np.unique(np_code[:,4]) 
    df_code= pd.DataFrame(clean_code)
    return df_code.to_excel('clean_zip.xls',encoding='cp949')
# clean()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18849774866966845, 'fraction_numerical': 0.1504297994269341, 'mean_word_length': 2.8541009463722395, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '127297', 'license': 'MIT', 'n_tokens_mistral': 2635, 'n_tokens_neox': 2563, 'n_words': 341, 'path': 'preprocessing/openApi (DK).ipynb', 'repo_name': 'dongkoull/Bigdata_contest', 'size': '4922'}