네이버 부동산 크롤링 GitHub - neibeo budongsan keulolling GitHub

naver 부동산 크롤러는 bot을 차단해놓아서 user agent 값을 지정하여 진행했다.
json으로 reponse되기에 json과 requests 모듈을 사용했다.

향후 Slack-bot으로 개발하여 slash command로 오갈 수 있도록 하고자한다.

golang으로 하다가 python으로 하니 내겐 더 간편하다.
go rutine 및 system program 등 golang이 이점이 있는 부분을 내가 사용하지 못하고 내가 작성하는 자동화 툴 같은 경우 큰 성능을 필요로 하지 않기에 python이 내게는 맞는 것 같다. 다음에 golang이 필요로 하는 경우에 또 공부해서 잘 적용해 보았으면 좋겠다

import requests, json def crawler(url): headers = {'User-Agent' : 'yjwang'} params = {'tradeType':'B1'} resp = requests.get(url, headers = headers) raw = resp.json() count = 0 while count < len(raw['articleList']): # 항목별로 json value가 없는 경우가 있어 예외 처리 진행 try: print("단지 이름 : ", raw['articleList'][count]['articleName']) print("매물 종류 : ", raw['articleList'][count]['tradeTypeName']) print("층 : ", raw['articleList'][count]['floorInfo']) print("금액 : ", raw['articleList'][count]['dealOrWarrantPrc']) print("평수 : ", str(raw['articleList'][count]['area2'])+" m**2") print("향 : ", raw['articleList'][count]['direction']) print("매물 일자 : ", raw['articleList'][count]['articleConfirmYmd']) print("설명 : ", raw['articleList'][count]['articleFeatureDesc']) print("태그 : ", raw['articleList'][count]['tagList']) print("동 : ", raw['articleList'][count]['buildingName']) print("링크 : ", raw['articleList'][count]['cpPcArticleUrl']) print("이미지 : ","//landthumb-phinf.pstatic.net"+raw['articleList'][count]['representativeImgUrl']) except: pass print("\n") count += 1 urls = [ # 신나무실 '//new.land.naver.com/api/articles/complex/2588?realEstateType=APT%3AABYG%3AJGC&tradeType=B1&tag=%3A%3A%3A%3A%3A%3A%3A%3A&rentPriceMin=0&rentPriceMax=900000000&priceMin=0&priceMax=33000&areaMin=0&areaMax=900000000&oldBuildYears&recentlyBuildYears&minHouseHoldCount&maxHouseHoldCount&showArticle=false&sameAddressGroup=true&minMaintenanceCost&maxMaintenanceCost&priceType=RETAIL&directions=&page=1&complexNo=2588&buildingNos=&areaNos=&type=list&order=rank', # 벽적골 8단지 주공 '//new.land.naver.com/api/articles/complex/3988?realEstateType=APT%3AABYG%3AJGC&tradeType=B1&tag=%3A%3A%3A%3A%3A%3A%3A%3A&rentPriceMin=0&rentPriceMax=900000000&priceMin=0&priceMax=33000&areaMin=0&areaMax=900000000&oldBuildYears&recentlyBuildYears&minHouseHoldCount&maxHouseHoldCount&showArticle=false&sameAddressGroup=true&minMaintenanceCost&maxMaintenanceCost&priceType=RETAIL&directions=&page=1&complexNo=3988&buildingNos=&areaNos=1%3A3%3A2&type=list&order=rank', # 벽적골 롯데 '//new.land.naver.com/api/articles/complex/3638?realEstateType=APT%3AABYG%3AJGC&tradeType=B1&tag=%3A%3A%3A%3A%3A%3A%3A%3A&rentPriceMin=0&rentPriceMax=900000000&priceMin=0&priceMax=33000&areaMin=0&areaMax=900000000&oldBuildYears&recentlyBuildYears&minHouseHoldCount&maxHouseHoldCount&showArticle=false&sameAddressGroup=true&minMaintenanceCost&maxMaintenanceCost&priceType=RETAIL&directions=&page=1&complexNo=3638&buildingNos=&areaNos=3&type=list&order=rank', # 벽적골 우성 '//new.land.naver.com/api/articles/complex/12045?realEstateType=APT%3AABYG%3AJGC&tradeType=B1&tag=%3A%3A%3A%3A%3A%3A%3A%3A&rentPriceMin=0&rentPriceMax=900000000&priceMin=0&priceMax=33000&areaMin=0&areaMax=900000000&oldBuildYears&recentlyBuildYears&minHouseHoldCount&maxHouseHoldCount&showArticle=false&sameAddressGroup=true&minMaintenanceCost&maxMaintenanceCost&priceType=RETAIL&directions=&page=1&complexNo=12045&buildingNos=&areaNos=2%3A1&type=list&order=rank' ] for url in urls: crawler(url)

Ver. Jupyter Notebook (Anaconda3)

▶ crawler_naver 부동산

수집: 대지 형태, 가격, 면적 등

코딩: github

JeongJaeyoung0/crawler

Contribute to JeongJaeyoung0/crawler development by creating an account on GitHub.

github.com

2021.05.14 # crawler_naver 부동산 step 1. 네이버 부동산 > 토지 > 매매 > 대지 형태, 가격, 면적 크롤링 > 판다스화 > 엑셀저장 pwd ### step 0. 준비 import sys # 시스템 import os # 시스템 import pandas as pd # 판다스 : 데이터분석 라이브러리 import numpy as np # 넘파이 : 숫자, 행렬 데이터 라이브러리 from bs4 import BeautifulSoup # html 데이터 전처리 from selenium import webdriver # 웹 브라우저 자동화 import time # 시간 지연 from tqdm import tqdm_notebook # 진행상황 표시 address_list = [] #주소 text_list = [] # 대 deal_list = [] # 거래 방식 won_list = [] # 가격 kind_list = [] # 토지 area_list = [] # 면적 ### step 1. 부동산 크롤링 # 크롬 웹브라우저 실행 driver = webdriver.Chrome(r"C:\Users\3\Desktop\정재영\exe\chromedriver.exe") # 사이트 주소 driver.get("//new.land.naver.com/offices?ms=36.3615444,127.3784552,14&a=SG:SMS:GJCG:APTHGJ:GM:TJ&e=RETAIL") time.sleep(2) # 토지 클릭 driver.find_element_by_xpath("/html/body/div[2]/div/div[1]/a[4]/span/em[4]").click() time.sleep(1) driver.find_element_by_css_selector(".filter_btn_select").click() driver.find_element_by_xpath("/html/body/div[2]/div/section/div[1]/div/div[1]/div/div[1]/div/ul/li[2]/label").click() driver.find_element_by_css_selector(".btn_close").click() # 상가, 사무실, 공장/창고, 지식산업센터, 건물 클릭으로 해제 driver.find_element_by_css_selector("#type0").click() driver.find_element_by_css_selector("#type1").click() driver.find_element_by_css_selector("#type2").click() driver.find_element_by_css_selector("#type3").click() driver.find_element_by_css_selector("#type4").click() time.sleep(1) # 시 클릭 driver.find_element_by_xpath("/html/body/div[2]/div/section/div[2]/div/div[1]/div/div/a/span[1]").click() time.sleep(0.5) # 대전시 클릭 driver.find_element_by_xpath("/html/body/div[2]/div/section/div[2]/div/div[1]/div/div/div/div[2]/ul/li[5]/label").click() time.sleep(0.5) # 구 목록 가져오기 address1 = driver.find_element_by_css_selector(".area_list--district").text.split() # 구 클릭 for i1 in range(1, len(address1)+1): time.sleep(0.5) #element1 = "/html/body/div[2]/div/section/div[2]/div[2]/div[1]/div/div/div/div[2]/ul/li[{}]/label".format(i1) element1 = "/html/body/div[2]/div/section/div[2]/div/div[1]/div/div/div/div[2]/ul/li[{}]/label".format(i1) driver.find_element_by_xpath(element1).click() time.sleep(0.5) # 동 목록 가져오기 address2 = driver.find_element_by_css_selector(".area_list--district").text.split() # 동 클릭 for i2 in range(1, len(address2)+1): #element2 = "/html/body/div[2]/div/section/div[2]/div[2]/div[1]/div/div/div/div[2]/ul/li[{}]/label".format(i2) element2 = "/html/body/div[2]/div/section/div[2]/div/div[1]/div/div/div/div[2]/ul/li[{}]/label".format(i2) driver.find_element_by_xpath(element2).click() time.sleep(1) # 크롤링 item = driver.find_element_by_css_selector(".item_list.item_list--article").text item_text = item.split('\n') ok_num = list(filter(lambda x: ('m²' in item_text[x]), range(len(item_text)))) # 대 for i in ok_num: text_list.append(item_text[i-2]) # 거래 방식 for i in ok_num: deal_list.append(item_text[i-1][0:2]) # 가격 for i in ok_num: price = item_text[i-1][2:] # 가격에 억 이하 단위가 없을 시 0000 삽입 if price[-1] =='억': price = price + '0000' # (억 , 띄워쓰기) 제거 won = ''.join( x for x in price if x not in '억, ') # 원 단위로 변환 won += '0000' won_list.append(int(won)) # 토지 for i in ok_num: kind_list.append(item_text[i][0:2]) # 면적 for i in ok_num: area = item_text[i][2:-2] area_list.append(int(area)) # 주소 저장 address = address1[i1-1], address2[i2-1] for i in ok_num: address_list.append(address) print(address1[i1-1], address2[i2-1], len(address_list), len(text_list), len(deal_list), len(won_list), len(kind_list), len(area_list)) driver.find_element_by_xpath("/html/body/div[2]/div/section/div[2]/div[2]/div[1]/div/div/a/span[3]").click() time.sleep(0.5) driver.find_element_by_xpath("/html/body/div[2]/div/section/div[2]/div[2]/div[1]/div/div/a/span[2]").click() time.sleep(0.5) driver.find_element_by_xpath("/html/body/div[2]/div/section/div[2]/div[2]/div[1]/div/div/a/span[2]").click() df = pd.DataFrame({'주소':address_list, '구분':text_list, '거래방식':deal_list, '가격':won_list, '대지형태':kind_list, '면적':area_list}) df df.info() df.to_excel('crawler_naver 부동산.xlsx')

Toplist

최신 우편물

태그