Member-only story
API Tutorial: How to use Bing Web Search API in Python
Python tutorial on how to get web search results
Need to get web search results fast? Microsoft Bing’s Web Search API is the right application for just that. Bing’s Web Search API combs billions of webpages, images, videos, and news with a single API call.
The Bing Web Search API is a RESTful Web service compatible with most programming languages. In this article, we will cover how to call the API using Python.
Use Case
For our use case, we will extract the ZPID field from Zillow listed properties. The ZPID is the unique identifier for properties listed on Zillow’s website.
Real estate investors and wholesalers need property details to evaluate deals. It is common to work with off-market properties, which may have limited property information.
Off-market properties are homes that are not for sale, or that is for sale but not listed publicly. — Bungalow
This could be a list of home owners that are in pre-foreclosure and motivated to sell their homes. Investors receive a list of the properties in pre-foreclosure, but require more property details.
Property details like price history, lot size, and zoning can be found in web scraping tools like Apify.
In order to get Zillow property details for an address, the ZPID is required. This is where Bing Web Search API comes in! :)
Setup
To get started you will need:
- Bing Web Search API subscription key
Sign up for Bing Web Search API
- Go to Bing Web Search API and click “TRY NOW”
- Sign into your Microsoft Azure account or create a new account
- Select “Start with an Azure free trial”
- Agree to the “subscription agreement” if prompted
- Add a payment method (you will not be charged if you stay within Free trial limits — 1,000 API calls a month)
- Name your Bing Search API resource
- At the home page of your resource go to “Manage keys”
- Under “Manage Keys” click “Show Keys” and copy the Key 1
- Key 1 will be your subscription key
Setup Python environment
To call the API we require an environment set up to write Python code.
I highly suggest to use Google Colab. It is a cloud based environment that is free to use. There is no local set up required. Follow the Getting Started With Google Colab tutorial for more information.
Python Tutorial
Check out Quickstart: Use Python to call the Bing Web Search API for additional information.
I. Install packages
!pip install requests
II. Import Packages
# HTTP library - https://docs.python-requests.org/en/latest/
import requests
III. Locals & Constants
Create a variable to store your API key as a string type object.
# subscription key
bing_subscription_key = <enter your key here>
VI. Function
Create a function that requires two parameters:
- Subscription Key — specific to the resource
- Query — your own web search string
def bing_web_search(subscription_key, query):
# set parameters
search_url = "https://api.bing.microsoft.com/v7.0/search"
headers = {"Ocp-Apim-Subscription-Key": subscription_key}
params = {
"q": query,
"textDecorations": True,
"textFormat": "HTML"} # get response
response = requests.get(search_url, headers=headers, params=params)
response.raise_for_status()
return response.json()
V. API Call
We wan to obtain the ZPID. It is at the end of the URL for the property:
“https://www.zillow.com/homedetails/11226-Spring-Point-Cir-Riverview-FL-33579/125824629_zpid/”
The ZPID for this property is 125824629.
Next, create a string object for the home address. Add “ zillow home details” to the end of the string. This will sort the Zillow result to the top of the search list.
Call the API with both parameters.
# call API
home_address = '11226 Spring Point Cir, Riverview, FL 33579'
search_results = bing_web_search(
subscription_key,
query= home_address + ' zillow home details')
In the search results select the first URL in the list of URLs. It should be the Zillow property link.
url = search_results['webPages']['value'][0]['url']
Next, get the ZPID by splitting the URL to grab the last element in the list.
zpid = [x for x in url.split('/') if 'zpid' in x][0].split('_')[0]
Output:
zpid = '125824629'
Awesome! We now have the ZPID for the property address we searched in the Bing Web Search API.
Conclusion
Bing’s Web Search API is an excellent resource to receive information on search results. It is free to use with options for pricing tiers for high volume searches.
Find more information on pricing tiers view Bing’s resource page: