← Back to list

Hotels API Functions

Hey all, so basically this post talks about how to extract code from the Hotels API. If you want to get data on hotels and reviews given by…

Attwood · 2022-04-03 12:09 · 0 claps · 4.1 min read
#hotels-api #python #rapidapi #data-scraping #hotel-reviews
Open on Medium ↗
Wiki topics: ✈️ · Travel

Hotels API Functions

Hey all, so basically this post talks about how to extract code from the Hotels API. If you want to get data on hotels and reviews given by their customers then use the below functions to quickly scrape some data.

Taken from Unsplash — Marten Bjork

Taken from Unsplash — Marten Bjork

Rapidapi.com

Subscription to this API is available via Rapidapi, with an account being required. Once you are registered you will be able to subscribe to different APIs.

API Dashboards and Subscription

To subscribe to the Hotels API you need to navigate to its page. The page dashboard will be very similar to the image below.

Screenshot, credit (writer)

Screenshot, credit (writer)

To the left you have the API endpoints. In the middle you have the ability to test endpoints, and to the right you have code snippets for the endpoint you wish to test (in a variety of different programming languages). We will be using Python and the requests package.

To subscribe to the API click the blue subscribe to test button. This will take you to a new page where you can select the free or paid subscription. Note — once subscribed this blue button becomes Test Endpoint, which is the case for the image above as I am already subscribed. Also Note — there are 500 free monthly calls.

Subscribing will provide you with an X-RapidAPI-Key which is added to your code snippet. This is your secret key.

Endpoints

In this project we will use three of the endpoints:

  • locations/v2/search — searches a list of hotels in a given location. The negative with this endpoint is that it only provides 10 suggestions of hotels in that area.
  • properties/list — searches availability of a hotel for a given date. However it also provides Id codes for various other hotels in the same location.
  • reviews/v2/list — provides reviews of hotels.

To use one of the endpoints simply copy and paste the code snippet into a text editor. I am using Python and Jupyter Notebooks.

Functions

After playing around with the API and the three above-mentioned endpoints I came up with the following functions which enable the creation of a data frame of reviews for hotels in a given location.

The basic idea is:

  • I take the first suggested hotel from the the locations/v2/search endpoint.
  • Its hotel Id is then used in the properties/list endpoint to get an extensive list of hotels in the area.
  • For each of these hotels, the hotel Id is used to search for reviews in the reviews/v2/list endpoint.
  • Information on the hotel (name, location, cost for one night, as well as review information is then combined into a data frame).

Function 1 — Hotels

Function 1 takes the first hotel Id suggested for the given location and uses the properties/list endpoint to get a list of hotels close by. Metadata on the hotels is saved to a data frame.

[embed]

However, one problem with Function 1 is that we are limited to 25 hotels as the function can’t go through more than one page of website results. To solve this problem Function 2 is developed. Note — to run Function 2 you still need to load Function 1.

Function 2 — Loop

Function 2 solves the above mentioned problem by looping through a defined number of pages of hotel results. The Page parameter minus 1 is the number of pages looped through (e.g. if page = 4, the function looks at 3 pages of results), and Size controls the number of hotels on each page of results. So if we have loop_hotels(page = 4, size = 10, location = ‘New York’) we will get results for 30 hotels ((4–1) 10)*.

[embed]

An example of the function output is shown below.

Winchester = loop_hotels(page = 3, size = 5, location = 'Winchester')

Screenshot, credit (writer)

Screenshot, credit (writer)

Function 3 — Reviews

To get the reviews for the hotels we create Function 3 which calls the reviews/v2/list endpoint for each of the hotel Ids in the Function 2 data frame. The reviews are saved to a new data frame and then merged on hotel id with the Function 2 data frame.

[embed]

Key points to note here are the delete hotels with 0 reviews section. Prior to including this, if a search returned hotels with 0 reviews then the function wouldn’t work.

We can run hotel_reviews and get the below output. Note how the columns between name and price_info are repeated for the first rows. This is because all of those rows relate to reviews for the same hotel. Rating is the first review specific column.

hotel_reviews(page = 3, size = '5', location = 'Cardiff')

Screenshot, credit (writer)

Screenshot, credit (writer)

Summary

With these three functions and the subscription to the Hotels API we can scrape a data frame of reviews for various hotels in a given location. Note — you need to load all three functions to get the final data frame of reviews.

Doing this project I learnt a lot about how to navigate and loop through a JSON dictionary, especially with nested dictionaries. I plan to write a post in the next few days identifying some of my key takeaways here.

Additionally, the next step of this project will be to run some NLP sentiment analysis on the hotel review data.

Feel free to use the function code in your own projects, however, please give credit with a link to my Github and Medium page (they took a good couple of evenings work to develop). If you have any questions feel free to drop a comment below. Many thanks :D


메타데이터
post_id
a6a5dd228607
slug
hotels-api-functions-a6a5dd228607
url
https://medium.com/@attwood1910/hotels-api-functions-a6a5dd228607
canonical_url
https://medium.com/@attwood1910/hotels-api-functions-a6a5dd228607
author_url
https://medium.com/@attwood1910
status
ok
fetched_at
2026-09-05 01:22:56