YJWANG

[Python] 부동산 크롤러-2 본문

90.Programming

[Python] 부동산 크롤러-2

왕영주 2020. 12. 22. 11:45

크롤러-1 에서 가져온 dict를 flask_table을 이용해 html table로 변형하는 코드이다.

import requests, json
from flask_table import Table, Col

class ItemTable(Table):
    classes = ['table', 'table-striped', 'table-bordered', 'table-condensed']
    articleName = Col('단지')
    buildingName = Col('동')
    tradeTypeName = Col('매물 종류')
    dealOrWarrantPrc = Col('금액')
    floorInfo = Col('층')
    area2 = Col('평수')
    articleConfirmYmd = Col('매물 일자')
    articleFeatureDesc = Col('설명')
    tagList = Col('태그') 
    cpPcArticleUrl = Col('링크')
    #representativeImgUrl = Col('이미지 링크')

# header 담아서 url에 get 요청 진행 후
# json을 dict 형태로 변환해서 저장

url = 'https://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'
headers = {'User-Agent' : 'yjwang'}

resp = requests.get(url, headers = headers).json()

try:
    table = ItemTable(resp['articleList'])
except:
    pass

print(table.__html__())

# html 결과를 아래와 같이 파일로 저장
f = open('./test.html', 'w')
f.write(table.__html__())
f.close()

반응형