Zigbang 원룸 매물 데이터 수집
동이름으로 매물 데이터를 수집해보겠습니다 !
▽ 과정
동이름으로 위도 경도 구하기 → 위도 경도로 geohash 알아내기
→ geohash로 매물 아이디 가져오기 → 매물 아이디로 매물 정보 가져오기
(1) 동이름으로 위도 경도 구하기
총 세단계를 거쳐아 한다.
- URL
- requests > response : json(str)
- json(str) > lat, lng

No.1 부동산 앱, 직방
여기를 눌러 링크를 확인하세요.
www.zigbang.com
아파트 빌라, 투룸, 원룸 등.. 다양한 옵션이 있지만 그 중 원룸 데이터를 수집 !


Request URL은 한글이 있기 때문에 decode해서 사용해야 한다
디코더/인코더 사이트
https://meyerweb.com/eric/tools/dencoder/\

URL Decoder/Encoder
meyerweb.com
import pandas as pd
import requests
# 1. URL
address = '논현동'
url = f'https://apis.zigbang.com/v2/search?leaseYn=N&q={address}&serviceType=원룸'
# 2. requests > response : json(str)
response = requests.get(url)
# 3. json(str) > lat, lng
data = response.json()['items'][0]
lat, lng = data['lat'], data['lng']
lat, lng
address는 망원동 외에도 다른지역도 검색 가능하도록 변수로 설정

response의 items에서 필요한 lat, lng만 가져온다

반복검색하면 안뜨는 경우가 있어서
시크릿모드로 사용하자
2. 위도 경도로 geohash 알아내기
※ Geohash란....?
GeoHash란 사각형의 격자구조를 이용하여 지구상의 위치를 고유하게 식별할 수 있는 고유 지리 ID
→ 임의의 장소에 대해 그 장소를 나타내는 고유의 ID가 있다는 뜻

!pip install geohash2
import geohash2
# precision: 클수록 영역이 작아짐
geohash = geohash2.encode(lat, lng, precision=5)
geohash
3. geohash로 매물 아이디 가져오기

Requests URL 가져오기
밑줄친부분 보면 items들이 geohash값을 포함하고 있음을 알 수 있다

Preview를 보면 어떻게 이루어져 있는지 확인 가능!
lat, lng, item_id......
url = f'https://apis.zigbang.com/v2/items?deposit_gteq=0&domain=zigbang&geohash={geohash}&needHasNoFiltered=true&\ rent_gteq=0&sales_type_in=전세|월세&service_type_eq=원룸'
response = requests.get(url)
response
# geohash 값으로 아이템 아이디 가져옴(매물 아이디)
data = response.json()['items']
ids = [item['item_id'] for item in data ]
ids[:5]
Preview에서 본 것처럼 items를 json포맷으로 data에 저장한 후,
item_id를 가져온다

4. 매물 아이디로 매물 정보 가져오기

Request URL 가져오기

Request Method가 POST라서 params에 데이터를 담아 보내야한다

list - Preview를 보면 어떤 내용이 담겨 있는지 알 수 있음
url = 'https://apis.zigbang.com/v2/items/list'
params = {
'domain' : 'zigbang',
'withCoalition' : 'true',
'item_ids' : ids[:900], #아이템 데이터의 갯수를 999개 까지 사용 가능
}
response = requests.post(url, params)
response

data = response.json()['items']
df = pd.DataFrame(data)
# 원하는 정보만 가져오기
colums = ['item_id', 'sales_type', 'deposit', 'rent', 'size_m2', 'floor', 'building_floor', 'title', 'address', 'status', 'service_type', 'tags', 'address1', 'manage_cost']
df = df[colums]
df = df[df['address'].str.contains(address)].reset_index(drop=True)
df.tail(2)

'AIVLE' 카테고리의 다른 글
| Web Crwaling ⑥ - selenium (0) | 2023.02.21 |
|---|---|
| Web Crwaling ⑤ - 네이버 연관 검색어 수집 (1) | 2023.02.21 |
| Web Crwaling ③ - 다음 환율 데이터 수집 (0) | 2023.02.19 |
| Web Crwaling ② - 네이버 주가 데이터 수집 (1) | 2023.02.17 |
| Web Crwaling ① - basic (0) | 2023.02.17 |