Categories: '교육'

[고급] 부동산 정보 필터 고도화 – 네이버 매물 정리하기 2

[고급] 부동산 정보 필터 고도화 – 네이버 매물 정리하기 편에 이어서 추가적으로 결과 값을 조금 더 디테일하게 정리해보려고 합니다. https://fin.land.naver.com/complexes/106861?tab=complex-info

네이버페이 부동산

네이버페이 부동산

m.land.naver.com

여기에서 보면 우리 데이터와 일부 맞지 않는 부분을 확인 할 수 있습니다. 바로 공급면적, 전용면적이 실제 매물에 나와 있는 면적과 다르다는 것입니다. 

[고급] 부동산 정보 필터 고도화 – 네이버 매물 정리하기 2

그 이유는 바로 네이버 면적 정보 부분에서 면적 정보를 각각 클릭해야만 해당 면적에 대한 정보를 가져오게 되는데, 우리가 크롤링했던 공급면적, 전용면적 등의 정보는 가장 처음 나오는 면적에 대한 정보를 끌어 왔기 때문이죠, 따라서 면적이라는 부분의 데이터를 기준으로 맞춰서 해당 공급면적을 찾아내고, 그에 맞는 전용면적 ~ 방/욕실에 대한 정보를 수정해야겠습니다.

[고급] 부동산 정보 필터 고도화 – 네이버 매물 정리하기 2

[고급] 부동산 정보 필터 고도화 – 네이버 매물 정리하기 2

[고급] 부동산 정보 필터 고도화 – 네이버 매물 정리하기 2

오늘 글은 시리즈로 구성된 기본편을 기본으로 하고 있습니다. 아직 기본편을 못 보신 분들이라면 아래 글을 한번 읽어 주세요!

2024.09.15 – [부동산/자동화 프로젝트] – 부동산 매물 정보 수집하기 – 부동산 데이터 네이버 부동산 크롤링 및 가공 #1

2024.09.15 – [부동산/자동화 프로젝트] – 부동산 매물 정보 수집하기 – 부동산 데이터 네이버 부동산 크롤링 및 가공 #2

2024.09.15 – [부동산/자동화 프로젝트] – 부동산 매물 정보 수집하기 – 부동산 데이터 네이버 부동산 크롤링 및 가공 #3

2024.09.17 – [부동산/자동화 프로젝트] – [고급] 부동산 정보 필터 고도화 – 네이버 매물 정리하기

이 부분은 동적 네트워크를 사용해야 해서, 현재 데이터에서만 정리해볼까 합니다. 현재 정리되어 있는 전체코드는 아래와 같습니다.

from google.colab import drive
import requests
import json
import pandas as pd
from datetime import datetime
from bs4 import BeautifulSoup

# Google Drive 마운트
drive.mount('/content/drive')

# 법정동 코드를 가져오는 함수
def get_dong_codes_for_city(city_name, sigungu_name=None, json_path='/content/drive/MyDrive/district.json'):
    try:
        with open(json_path, 'r', encoding='utf-8') as file:
            data = json.load(file)
    except FileNotFoundError:
        print(f"Error: The file at {json_path} was not found.")
        return None, None

    for si_do in data:
        if si_do['si_do_name'] == city_name:
            if sigungu_name and sigungu_name != '전체':
                for sigungu in si_do['sigungu']:
                    if sigungu['sigungu_name'] == sigungu_name:
                        return [sigungu['sigungu_code']], [
                            {'code': dong['code'], 'name': dong['name']} for dong in sigungu['eup_myeon_dong']
                        ]
            else:  # 시군구 '전체'
                sigungu_codes = [sigungu['sigungu_code'] for sigungu in si_do['sigungu']]
                dong_codes = [
                    {'code': dong['code'], 'name': dong['name']}
                    for sigungu in si_do['sigungu']
                    for dong in sigungu['eup_myeon_dong']
                ]
                return sigungu_codes, dong_codes
    return None, None

# 아파트 코드 리스트 가져오기
def get_apt_list(dong_code):
    down_url = f'https://new.land.naver.com/api/regions/complexes?cortarNo={dong_code}&realEstateType=APT&order='
    header = {
        "Accept-Encoding": "gzip",
        "Host": "new.land.naver.com",
        "Referer": "https://new.land.naver.com/complexes/102378",
        "Sec-Fetch-Dest": "empty",
        "Sec-Fetch-Mode": "cors",
        "Sec-Fetch-Site": "same-origin",
        "User-Agent": "Mozilla/5.0"
    }

    try:
        r = requests.get(down_url, headers=header)
        r.encoding = "utf-8-sig"
        data = r.json()

        if 'complexList' in data and isinstance(data['complexList'], list):
            df = pd.DataFrame(data['complexList'])
            required_columns = ['complexNo', 'complexName', 'buildYear', 'totalHouseholdCount', 'areaSize', 'price', 'address', 'floor']

            for col in required_columns:
                if col not in df.columns:
                    df[col] = None

            return df[required_columns]
        else:
            print(f"No data found for {dong_code}.")
            return pd.DataFrame(columns=required_columns)

    except Exception as e:
        print(f"Error fetching data for {dong_code}: {e}")
        return pd.DataFrame(columns=required_columns)

# 아파트 코드로 상세 정보를 가져오는 함수 (매매 정보 추가)
def get_apt_details(apt_code):
    details_url = f'https://fin.land.naver.com/complexes/{apt_code}?tab=complex-info'
    article_url = f'https://fin.land.naver.com/complexes/{apt_code}?tab=article&tradeTypes=A1'
    
    header = {
        "Accept-Encoding": "gzip",
        "Host": "fin.land.naver.com",
        "Referer": "https://fin.land.naver.com/",
        "Sec-Fetch-Dest": "empty",
        "Sec-Fetch-Mode": "cors",
        "Sec-Fetch-Site": "same-origin",
        "User-Agent": "Mozilla/5.0"
    }
    
    try:
        # 기본 정보 가져오기
        r_details = requests.get(details_url, headers=header)
        r_details.encoding = "utf-8-sig"
        soup_details = BeautifulSoup(r_details.content, 'html.parser')
        
        # 아파트 이름 추출
        apt_name_tag = soup_details.find('span', class_='ComplexSummary_name__vX3IN')
        apt_name = apt_name_tag.text.strip() if apt_name_tag else 'Unknown'

        # 기본 정보 딕셔너리
        detail_dict = {'complexNo': apt_code, 'complexName': apt_name}
        
        # 기본 상세 정보 추출 (공급면적, 전용면적, 방/욕실 등)
        detail_items = soup_details.find_all('li', class_='DataList_item__T1hMR')
        for item in detail_items:
            term = item.find('div', class_='DataList_term__Tks7l').text.strip()
            definition = item.find('div', class_='DataList_definition__d9KY1').text.strip()
            if term in ['공급면적', '전용면적', '해당면적 세대수', '현관구조', '방/욕실', '위치', '사용승인일', '세대수', '난방', '주차', '전기차 충전시설', '용적률/건폐율', '관리사무소 전화', '건설사']:
                detail_dict[term] = definition
        
        # 매물 정보 가져오기
        r_article = requests.get(article_url, headers=header)
        r_article.encoding = "utf-8-sig"
        soup_article = BeautifulSoup(r_article.content, 'html.parser')
        
        # 매물 리스트
        listings = []
        for item in soup_article.find_all('li', class_='ComplexArticleItem_item__L5o7k'):
            listing = {}
            
            # 매물 이름
            name_tag = item.find('span', class_='ComplexArticleItem_name__4h3AA')
            listing['매물명'] = name_tag.text.strip() if name_tag else 'Unknown'
            
            # 매매 가격
            price_tag = item.find('span', class_='ComplexArticleItem_price__DFeIb')
            listing['매매가'] = price_tag.text.strip() if price_tag else 'Unknown'
            
            # 면적, 층수, 방향
            summary_items = item.find_all('li', class_='ComplexArticleItem_item-summary__oHSwl')
            if len(summary_items) >= 4:
                listing['면적'] = summary_items[1].text.strip() if len(summary_items) > 1 else 'Unknown'
                listing['층수'] = summary_items[2].text.strip() if len(summary_items) > 2 else 'Unknown'
                listing['방향'] = summary_items[3].text.strip() if len(summary_items) > 3 else 'Unknown'
            
            # 이미지
            image_tag = item.find('img')
            listing['이미지'] = image_tag['src'] if image_tag else 'No image'
            
            # 코멘트
            comment_tag = item.find('p', class_='ComplexArticleItem_comment__zN_dK')
            listing['코멘트'] = comment_tag.text.strip() if comment_tag else 'No comment'
            
            # 각 매물마다 기본 상세 정보(공급면적, 방/욕실 등)를 매물에 추가
            combined_listing = {**detail_dict, **listing}
            listings.append(combined_listing)
        
        return listings
    
    except Exception as e:
        print(f"Error fetching details for {apt_code}: {e}")
        return []

# 아파트 정보를 수집하는 함수 (법정동 선택 가능)
def collect_apt_info_for_city(city_name, sigungu_name, dong_name=None, json_path='/content/drive/MyDrive/district.json'):
    sigungu_codes, dong_list = get_dong_codes_for_city(city_name, sigungu_name, json_path)

    if dong_list is None:
        print(f"Error: {city_name} not found in JSON.")
        return None

    all_apt_data = []
    dong_code_name_map = {dong['code']: dong['name'] for dong in dong_list}

    # 법정동 선택
    if dong_name and dong_name != '전체':
        dong_code_name_map = {k: v for k, v in dong_code_name_map.items() if v == dong_name}

    for dong_code, dong_name in dong_code_name_map.items():
        print(f"Collecting apartment codes for {dong_code} ({dong_name})")
        apt_codes = get_apt_list(dong_code)

        if not apt_codes.empty:
            for _, apt_info in apt_codes.iterrows():
                apt_code = apt_info['complexNo']
                print(f"Collecting details for {apt_code}")
                listings = get_apt_details(apt_code)
                
                if listings:
                    for listing in listings:
                        # 모든 매물 정보를 결합
                        listing['dong_code'] = dong_code
                        listing['dong_name'] = dong_name
                        all_apt_data.append(listing)
        else:
            print(f"No apartment codes found for {dong_code}")

    if all_apt_data:
        final_df = pd.DataFrame(all_apt_data)
        final_df['si_do_name'] = city_name
        final_df['sigungu_name'] = sigungu_name
        final_df['dong_name'] = dong_name if dong_name else '전체'
        
        # 엑셀 파일로 저장
        file_path = f'/content/drive/MyDrive/{city_name}_{sigungu_name}_apartments.xlsx'
        final_df.to_excel(file_path, index=False)
        print(f"Data saved to {file_path}")
    else:
        print("No data to save.")

# 함수 호출 예시
collect_apt_info_for_city("서울특별시", "강남구", "개포동")

자 다음편에서는 이 서비스를 스트림릿으로 연계해서 실제 사용자가 편하게 웹에서 선택하여 사용할 수 있도록 정리해보겠습니다.

 

urjent

Share
Published by
urjent

Recent Posts

안동맛집 현지인 추천 베스트 로컬 먹거리와 숨은 명소 총정리

안동맛집 탐방을 계획하고 있다면 단순히 유명세만 좇기보다 현지인들이 자주 찾는 로컬 식당과 전통 시장의 숨은…

14분 ago

국민연금 조기수령 조건 완벽 정리와 감액률 계산 방법

국민연금 조기수령 조건은 은퇴 후 소득이 끊겨 당장 생계가 어려운 가입자가 원래 정해진 나이보다 최대…

2시간 ago

안세영 나이 프로필 배드민턴 우승 기록 연봉 상금 총정리

안세영은 올림픽과 아시안게임, 세계선수권까지 모두 제패하며 대한민국 여자 배드민턴의 역사를 새로 쓰고 있는 세계적인 정상급…

3시간 ago

국민연금 조기수령나이 감액률 조건 신청방법 완벽 정리

국민연금 조기수령나이는 법정 수급 개시 나이보다 최대 5년 앞서 연금을 받을 수 있는 제도이지만 1년을…

3시간 ago

10월축제 추천 가을 여행지 총정리 전국 가볼만한곳 가을꽃 명소

전국 각지에서 형긋한 꽃내음과 함께 다채로운 즐길 거리를 선사하는 10월축제가 드디어 막을 올립니다. 아침저녁으로 쌀쌀해진…

5시간 ago

2026 추석 씨름대회 일정 중계, 추석장사씨름대회 장소 날짜

2026년 추석장사씨름대회는 다가오는 10월 5일부터 11일까지 일주일 동안 충청북도 괴산군에서 화려한 막을 올립니다. 매년 명절…

5시간 ago