An Introduction to Scraping Real Estate Data with rvest and RSelenium

In this tutorial, I will be explaining how to scrape real estate data with rvest and RSelenium. In this tutorial, we scraped indeed job postings and analysed what it takes to become a Data Scientist. Scraping data from Indeed was relatively easy because we could get all the information we needed by looking at the view source.

Sometimes however, the view source does not contain all the code we are seeing when loading a website. This is a problem because we cannot scrape the data with the rvest package when there is no location in view source for the specific element. In short, we are unable to access the code without support of a browser.

What we will be covering in this tutorial where we will be scraping real estate data with rvest and RSelenium is:

  • Why do we need RSelenium?
  • Scraping real estate data with RSelenium
  • Developing a working scraper step by step
  1. Identifying the URL and page structure
  2. Scraping all the links from each page result
  3. Scraping the necessary information  from each link
  4. Scraping real estate prices
  5. Scraping real estate street addresses
  6. Scraping square feet, bedrooms, bathrooms, age, and year built
  7. Putting all parts together into one big scraper

Let’s start with scraping real estate data with rvest and RSelenium

Why do we Need RSelenium?

When you look view source then you’ll see the HTML as it was delivered by the server without any modification by JavaScript for example. In order to see the website’s code in its current state you have to use RSelenium.

Selenium is a web automation tool. It opens up a browser and sees everything on the website that you are seeing. This means, when you are inspecting a website’s code, you are able to scrape all elements from there even if the website is dynamically altered by JavaScript.

This is very handy an leaves you with so much more opportunities to automatically get data from websites.

Scraping Real Estate Data with rvest and RSelenium

In order see how to use RSelenium, we looked at sotheby’s real estate postings. However, you can use any other real estate website that is dynamically altered and then use RSelenium to get the information you are interested in. Some websites do not allow web scraping and there are specific copyright laws to using their data. Therefore, always make sure you scrape carefully.

When you are on the website and are searching postings from a specific town, then you’ll see something like this:

RSelenium Real Estate

The pictures link to the details about the house/apartment. So, you are able to find the price, number of bedrooms, number of bathrooms, how old the house is, how much you have to pay in taxes each year, and the specific address of where the real estate is located. 

So far so good. However, when we click view source in our browser and want to collect all the links to scrape all the details of the real estate then we see this: 

RSelenium javascript

This means, that the links are being dynamically altered by javascript. So, there is unfortunately no way to get the links with only rvest.Rvest is not able to extract it because it only finds “JavaScript” instead of the actual link.

However, if we click to inspect the page then we can clearly see that the links are there:

RSelenium

This is where RSelenium comes into play and helps us to scrape the necessary information. It helps us to see the website in real-time. 

Developing a Working Scraper Step by Step

Step 1: Identifying the URL and Page Structure

The first step is to identify the URL of the page. Let say we would be interested in scraping real estate data in Vancouver, for condos below one million dollar. The URL looks like this:

Afterwards, we have to identify the page structure. This is where we look at how many page results the website shows and how the URL changes when clicking through the pages.

RSelenium

There are all in all 38 page result with 60 homes per page. In order to get the links for all 60*38 = 2280 home we do it like this:

Notice how the we are appending /page- to the URL. What we are doing is looping over 2, 3, 4, …, 38 and each time appending /page-2, /page-3, …, /page-38 to the URL with paste0(url, x). Then, we are storing all 2280 URLs in the urls  object. 

Step 2: Scraping all the Links from Each Page Result

Next, we are using RSelenium to get all 60 links from each URL we previously stored. If you have not installed RSelenium yet then now it is the perfect time to do it and open up the browser with:

Now, we are ready to get all the links. We will be storing the links in a data frame. The code below shows you how to do that:

The code above loops over each URL with a for loop. It opens up the URL, locates the elements with the specified XPath (have a look at the picture above where it shows a link and where it says class="plink")loops over all elements of class="plink" with sapply, and then extracts each link with the href attribute. Lastly, we are storing the links in the df_all data frame. That was not too hard was it? If you are confused about what an XPath is and how we got the links then check out this tutorial, where I go into more detail about web scraping and XPaths. 

Step 3: Scraping the Necessary Information  from each Link

Now, it is just a piece of cake to locate the information we need. 

Scraping Real Estate Data with rvestscraping real estate data with rvest

Step 3.1: Scraping Real Estate Prices

Rvest real estate web scraping

The price is located under the ul element and can be extracted with the following XPath //*[@class="price_social"]. The code would look like this:

Step 3.2: Scraping Real Estate Street Addresses

Rvest real estate address

The address is located under the span element and the h1 header. The XPath would be //*[@class="span8"]. The code to get the address looks like this:

Step 3.3: Scraping Square Feet, Bedrooms, Bathrooms, Age, and Year Built

Extract the next information from the website is very easy. There is a section called key facts which contains all the remaining information we need. We can do that with the code below:

And voila! We have all the information we need. Now it is time to put the different parts together into one big scraper that does the work for us. 

Step 4: Putting All Parts Together Into one big Scraper

And that’s it. We did some data cleaning inside the function and made some modification to make sure our function is not throwing an error when it cannot find elements such as age or taxes. 

Let me know in the comments below if this tutorial about scraping real estate data with rvest and RSelenium was helpful in getting you started with rvest and RSelenium. Also let me know what kind of question you want to answer with your new real estate data.

Questions that might be helpful to start a project would be:

  • What are the different price to rent ratios in different areas of a city?
  • What price for a house would be considered below market value?

It would be great to build different scrapers for multiple website. This would decrease bias in what kind of real estate data we are getting. For example, some websites are only posting luxury homes. Another selection bias would that websites are only posting data for a specific neighborhood in a city. 

If you are looking for other tutorials as well, here is a tutorial about how to scrape Zillow real estate postings. 

Disclaimer: Any code provided in this tutorials is for illustration and learning purposes only. The presence of this code does not imply that we encourage scraping or scrape the websites referenced in the code and tutorial. The tutorials only help illustrate the technique of programming web scrapers with R Studio, rvest, and RSelenium for various websites. 

Leave a Reply

Your email address will not be published. Required fields are marked *