Table of Contents
IntroductionThe real estate market is one of the most dynamic fields, where data scraping plays a major role not only for real estate business owners and agencies but also for regular customers. When we need to make the decision regarding buying or renting properties, the first thing we should do is a comparative analysis based on price, type of house, its size, location, etc. Therefore, we’re going to scrape the leading real estate marketplace called Zillow. There are several paid Zillow data scrapers in the market that you can buy and use, but in this article, we are going to scrape Zillow with the help of Python. So, if you have some coding skills and do not want to pay the extra money, let’s move forward to learn how to download data from Zillow.
Why Choose PythonAs we’ve mentioned above, if you have some coding skills and a bit of knowledge about web scraping, then you can develop your Zillow data scraper to extract the required data from Zillow. You can use any programming language to handle HTML files, but Python is widely used for developing scrapers. Some facts:
- BeautifulSoup and Scrapy are the most popular scraping-friendly frameworks based on Python.
- BeautifulSoup library provides a fast and highly effective data extraction.
- Python supports XPath.
- Great idioms are provided for searching, navigating, and modifying the parse tree.
- Other advanced web scraping libraries are available.
Scraping Zillow Using Python and LXML
Python tools you will needFor scraping Zillow with Python, it is required to have Python 3 and Pip installed. Follow the instructions below for the purpose
- For Linux users: http://docs.python-guide.org/en/latest/starting/install3/linux/
- For Mac users: http://docs.python-guide.org/en/latest/starting/install3/osx/
- Windows users go here: https://www.scrapehero.com/how-to-install-python3-in-windows-10/
Common stepsWe are going to search and scrape Zillow data based on a specific postal code: 02128. The whole scraping process contains the following steps:
- Conduct a search on Zillow by inserting the postal code.
- Get the search results URL https://www.zillow.com/homes/02128_rb/.
- Download HTML code through Python Requests.
- Parse the page through LXML.
- Export the extracted data to a CSV file.
Running the Zillow data scraperLet’s name the script zillow.py that will be used for the script name in a command line. So, to get the newest listings, we should run an appropriate script to sort the relevant arguments for the specific zip code. In the final step, a CSV file will be created in the same folder as the script.
Scrape Zillow Using Python and BeautifulSoupIn this part, we’ll just go through some useful insights that you can use while scraping Zillow.
Required librariesFor BeautifulSoup you need to install the required libraries, which can be done through the requirements.txt file. Just input the complete list in the file and run the pip install requirements.txt file.
Bypassing captchasLike many websites, Zillow also throws captchas. That’s why while deploying a request.get(url) function, it is required to add headers to the request function. See the below example:
Looping through URLsTo create variables, there are many ways to loop through URLs. Let’s try the simplest one. So, if you are planning to extract 5 pages’ data, you can create 5 soup variables and give them a unique title as in the below example.
Formatting dataTo make the extracted data more readable, just make some formatting jobs. So, we are going to:
- Convert columns
- Rearrange columns
- Drop null rows