Scraping real estate data with Python to find opportunities

In this tutorial, we’ll explore how to scrape real estate data from an API using Python’s requests library. We’ll also learn how to apply filters to retrieve potential bargain properties whose prices have recently dropped.


Introduction

When hunting for great real estate opportunities, one of the best indicators can be a recent price drop. Having a tool that quickly shows you only these properties can save you tons of time—and might help you scoop up a deal before everyone else notices!

In this post, we’ll:

  1. Discuss the basics of using requests to interact with a real estate API.
  2. Learn how to filter results using query parameters—particularly focusing on price variation queries.
  3. Parse and display the returned data in a concise format.

Requirements

  • Python 3 installed
  • A terminal or command-line prompt
  • Basic familiarity with the Python requests library
  • An API key (if required by the API)

Step 1: Understanding the API

The API we use might respond with data such as:

  • Property ID
  • Title or Address
  • Price
  • Location
  • Historical price changes
  • Other relevant information

Key Query Parameters

This API supports several query parameters that help us filter the results:

Parameter Type Description
includedDepartments[] array Filter by department(s). Example: departments/77
fromDate date Only retrieve properties listed (or updated) after this date.
propertyTypes[] array Filter by property type. Example: 0 for apartments, 1 for houses, etc.
transactionType string 0 for sale, 1 for rent, etc.
withCoherentPrice bool Only retrieve properties whose price is coherent with the market.
budgetMin number Minimum budget threshold.
budgetMax number Maximum budget threshold.
eventPriceVariationFromCreatedAt date Date from which an event of type price is created — inclusive.
eventPriceVariationMin number Minimum percentage of price variation (negative or positive).

We’ll especially focus on the eventPriceVariation parameters to find properties that have decreased in price.


Step 2: Crafting the Request

Below is a sample script using Python’s requests library to query the endpoint. Adjust the parameters and headers as needed, especially if an X-API-KEY is required.

<span>import</span> <span>requests</span>
<span>import</span> <span>json</span>
<span># 1. Define the endpoint URL </span><span>url</span> <span>=</span> <span>"</span><span>https://api.stream.estate/documents/properties</span><span>"</span>
<span># 2. Create the parameters </span><span>params</span> <span>=</span> <span>{</span>
<span>'</span><span>includedDepartments[]</span><span>'</span><span>:</span> <span>'</span><span>departments/77</span><span>'</span><span>,</span>
<span>'</span><span>fromDate</span><span>'</span><span>:</span> <span>'</span><span>2025-01-10</span><span>'</span><span>,</span>
<span>'</span><span>propertyTypes[]</span><span>'</span><span>:</span> <span>'</span><span>1</span><span>'</span><span>,</span> <span># 1 might represent 'apartment' </span> <span>'</span><span>transactionType</span><span>'</span><span>:</span> <span>'</span><span>0</span><span>'</span><span>,</span> <span># 0 might represent 'sale' </span> <span>'</span><span>withCoherentPrice</span><span>'</span><span>:</span> <span>'</span><span>true</span><span>'</span><span>,</span>
<span>'</span><span>budgetMin</span><span>'</span><span>:</span> <span>'</span><span>100000</span><span>'</span><span>,</span>
<span>'</span><span>budgetMax</span><span>'</span><span>:</span> <span>'</span><span>500000</span><span>'</span><span>,</span>
<span># Focusing on price variation </span> <span>'</span><span>eventPriceVariationFromCreatedAt</span><span>'</span><span>:</span> <span>'</span><span>2025-01-01</span><span>'</span><span>,</span> <span># since the beginning of the year </span> <span>'</span><span>eventPriceVariationMin</span><span>'</span><span>:</span> <span>'</span><span>10</span><span>'</span><span>,</span> <span># at least a 10% drop </span><span>}</span>
<span># 3. Define headers with the API key </span><span>headers</span> <span>=</span> <span>{</span>
<span>'</span><span>Content-Type</span><span>'</span><span>:</span> <span>'</span><span>application/json</span><span>'</span><span>,</span>
<span>'</span><span>X-API-KEY</span><span>'</span><span>:</span> <span>'</span><span><your_api_key_here></span><span>'</span>
<span>}</span>
<span># 4. Make the GET request </span><span>response</span> <span>=</span> <span>requests</span><span>.</span><span>get</span><span>(</span><span>url</span><span>,</span> <span>headers</span><span>=</span><span>headers</span><span>,</span> <span>params</span><span>=</span><span>params</span><span>)</span>
<span># 5. Handle the response </span><span>if</span> <span>response</span><span>.</span><span>status_code</span> <span>==</span> <span>200</span><span>:</span>
<span>data</span> <span>=</span> <span>response</span><span>.</span><span>json</span><span>()</span>
<span>print</span><span>(</span><span>json</span><span>.</span><span>dumps</span><span>(</span><span>data</span><span>,</span> <span>indent</span><span>=</span><span>2</span><span>))</span>
<span>else</span><span>:</span>
<span>print</span><span>(</span><span>f</span><span>"</span><span>Request failed with status code </span><span>{</span><span>response</span><span>.</span><span>status_code</span><span>}</span><span>"</span><span>)</span>
<span>import</span> <span>requests</span>
<span>import</span> <span>json</span>

<span># 1. Define the endpoint URL </span><span>url</span> <span>=</span> <span>"</span><span>https://api.stream.estate/documents/properties</span><span>"</span>

<span># 2. Create the parameters </span><span>params</span> <span>=</span> <span>{</span>
    <span>'</span><span>includedDepartments[]</span><span>'</span><span>:</span> <span>'</span><span>departments/77</span><span>'</span><span>,</span>
    <span>'</span><span>fromDate</span><span>'</span><span>:</span> <span>'</span><span>2025-01-10</span><span>'</span><span>,</span>
    <span>'</span><span>propertyTypes[]</span><span>'</span><span>:</span> <span>'</span><span>1</span><span>'</span><span>,</span>    <span># 1 might represent 'apartment' </span>    <span>'</span><span>transactionType</span><span>'</span><span>:</span> <span>'</span><span>0</span><span>'</span><span>,</span>    <span># 0 might represent 'sale' </span>    <span>'</span><span>withCoherentPrice</span><span>'</span><span>:</span> <span>'</span><span>true</span><span>'</span><span>,</span>
    <span>'</span><span>budgetMin</span><span>'</span><span>:</span> <span>'</span><span>100000</span><span>'</span><span>,</span>
    <span>'</span><span>budgetMax</span><span>'</span><span>:</span> <span>'</span><span>500000</span><span>'</span><span>,</span>
    <span># Focusing on price variation </span>    <span>'</span><span>eventPriceVariationFromCreatedAt</span><span>'</span><span>:</span> <span>'</span><span>2025-01-01</span><span>'</span><span>,</span>  <span># since the beginning of the year </span>    <span>'</span><span>eventPriceVariationMin</span><span>'</span><span>:</span> <span>'</span><span>10</span><span>'</span><span>,</span>  <span># at least a 10% drop </span><span>}</span>

<span># 3. Define headers with the API key </span><span>headers</span> <span>=</span> <span>{</span>
  <span>'</span><span>Content-Type</span><span>'</span><span>:</span> <span>'</span><span>application/json</span><span>'</span><span>,</span>
  <span>'</span><span>X-API-KEY</span><span>'</span><span>:</span> <span>'</span><span><your_api_key_here></span><span>'</span>
<span>}</span>

<span># 4. Make the GET request </span><span>response</span> <span>=</span> <span>requests</span><span>.</span><span>get</span><span>(</span><span>url</span><span>,</span> <span>headers</span><span>=</span><span>headers</span><span>,</span> <span>params</span><span>=</span><span>params</span><span>)</span>

<span># 5. Handle the response </span><span>if</span> <span>response</span><span>.</span><span>status_code</span> <span>==</span> <span>200</span><span>:</span>
    <span>data</span> <span>=</span> <span>response</span><span>.</span><span>json</span><span>()</span>
    <span>print</span><span>(</span><span>json</span><span>.</span><span>dumps</span><span>(</span><span>data</span><span>,</span> <span>indent</span><span>=</span><span>2</span><span>))</span>
<span>else</span><span>:</span>
    <span>print</span><span>(</span><span>f</span><span>"</span><span>Request failed with status code </span><span>{</span><span>response</span><span>.</span><span>status_code</span><span>}</span><span>"</span><span>)</span>
import requests import json # 1. Define the endpoint URL url = "https://api.stream.estate/documents/properties" # 2. Create the parameters params = { 'includedDepartments[]': 'departments/77', 'fromDate': '2025-01-10', 'propertyTypes[]': '1', # 1 might represent 'apartment' 'transactionType': '0', # 0 might represent 'sale' 'withCoherentPrice': 'true', 'budgetMin': '100000', 'budgetMax': '500000', # Focusing on price variation 'eventPriceVariationFromCreatedAt': '2025-01-01', # since the beginning of the year 'eventPriceVariationMin': '10', # at least a 10% drop } # 3. Define headers with the API key headers = { 'Content-Type': 'application/json', 'X-API-KEY': '<your_api_key_here>' } # 4. Make the GET request response = requests.get(url, headers=headers, params=params) # 5. Handle the response if response.status_code == 200: data = response.json() print(json.dumps(data, indent=2)) else: print(f"Request failed with status code {response.status_code}")

Enter fullscreen mode Exit fullscreen mode

Explanation of Important Parameters

eventPriceVariationMin = '-10'

This means you’re looking for at least a 10% price decrease.

eventPriceVariationMax = '0'

Setting this to 0 ensures you don’t include properties that have had any price increase or any variation above 0%. Essentially, you’re capturing negative or zero changes.

Tip: Adjust the min/max values to suit your strategy. For instance, -5 and 5 would include price changes within a ±5% range.

Potential Pitfalls & Considerations

  1. Authentication: Always ensure you’re using valid API keys. Some APIs also have rate limits or usage quotas.
  2. Error Handling: Handle cases where the API is down or parameters are invalid.
  3. Data Validation: The API might return incomplete data for some listings. Always check for missing fields.
  4. Date Formats: Make sure your fromDate and toDate are in a format the API recognizes (e.g., YYYY-MM-DD).
  5. Large Datasets: If the API returns hundreds or thousands of listings, you might need pagination. Check the API docs for pagination parameters like page or limit.

Wrap-Up

Now you have a basic Python script to scrape real estate data, focusing on properties that have seen a drop in price. This approach can be extremely powerful if you’re looking to invest in real estate or if you simply want to track market trends.

As always, tailor the parameters to your specific needs. You can expand this script to sort results by price, integrate advanced analytics, or even plug the data into a machine learning model for deeper insights.

Happy scraping, and may you find that hidden gem!


Further Reading

原文链接:Scraping real estate data with Python to find opportunities

© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享
Fight for the things you love no matter what you may face, it will be worth it.
不管你面对的是什么,为你所爱的而奋斗都会是值得的
评论 抢沙发

请登录后发表评论

    暂无评论内容