An interactive map, accurate as of 13/08/2018 showing property prices per square meter in various areas of Tallin:
Data shown is for 3-bedroom apartments (resource limitations). Green is less expensive, red is more expensive. Clicking on a data point will show a popup containing the actual price per square meter for that data point
As any house/apartment hunter knows, finding the perfect place to call home is an arduous and drawn-out process. In this show-and-tell article I’ve used python to scrape data from one of the most popular Estonian real-estate sites (https://kv.ee) and display the median price per square meter at different locations across Tallinn:
The above is a screenshot of the final result, which you can browse here:
Note: the map only shows results for 3-bedroom apartments due to resource limitations. Green is cheaper, red is more expensive
Tip: click on the individual data points to display a popup showing the actual price per square meter.
The actual code is posted at the end of this article. The main ingredients for this script were the python “requests” and “requests_html” modules. Admittedly, I could have used just one module but I did want to try out the HTML parsing capabilities of the requests_html module. For simplicity’s sake, the script outputs a static HTML file which can then be loaded into the browser or github pages like I did above. A more sophisticated approach would be to use a Python web framework like Flask to host the web page directly.
Scraping the data involved inspecting the web traffic between the browser and KV.EE, specifically when using the “Search by Map” functionality on the site. One the appropriate search filters are set, and the map is centered around the area you’d like to search within, pressing the “search” button issues a request via a URL similar to that shown on line 24 in the code below. The parameters I was particular interested in were the parameters describing the map area to search:
- nelng / nelat : north east longitude / latitude (the top right corner of the map)
- swlng / swlat : south west longitude / latitude (the bottom left corner of the map)
This allows us to issue different requests for the areas within which we’d like to scrape data, as was done in lines 154-185 from the code snippet below. The “get_area_objects” class method gets a list of object IDs representing apartments, and their corresponding co-ordinates.
At this stage, we have the co-ordinates for the apartments, but we need to get their price and area in order to calculate their price per meter squared. This is what the “get_object_details” class method does – and it is here that requests_html really shines since it makes it very easy to extract the data we require