Scrape Booking.com Prices with Python & BeautifulSoup: A Complete Guide (2026)

Discover how to scrape hotel prices from Booking.com using Python and BeautifulSoup. This tutorial provides step-by-step instructions and troubleshooting advice.

Scrape Booking.com Prices with Python & BeautifulSoup: A Complete Guide (2026)

Scrape Booking.com Prices with Python & BeautifulSoup: A Complete Guide (2026)

Web scraping is a powerful tool for collecting data from websites. In this tutorial, we will explore how to scrape hotel prices from Booking.com using Python and BeautifulSoup. This guide will walk you through the entire process, from setting up your environment to handling common errors.

Key Takeaways

  • Learn how to set up BeautifulSoup for web scraping.
  • Understand how to extract specific elements from a webpage.
  • Handle common issues when scraping dynamic websites.
  • Implement a simple script to scrape hotel prices from Booking.com.
  • Learn about ethical considerations and legalities of web scraping.

Scraping websites can be invaluable for gathering data for analysis, research, or personal projects. In this tutorial, we will focus on extracting the price of the first hotel from Booking.com search results. This type of data can be useful for price comparison, travel planning, or even building a price alert system.

Before embarking on this journey, it's important to understand both the technical and ethical aspects of web scraping. While this tutorial focuses on the technical side, always ensure you comply with the site's terms of service and legal regulations.

Prerequisites

To follow along with this tutorial, you will need the following:

  • Basic knowledge of Python programming
  • Python 3.8 or higher installed
  • Familiarity with HTML and CSS
  • Installed Python libraries: requests and BeautifulSoup
  • A modern web browser

Step 1: Install Required Libraries

First, ensure you have Python and pip installed. You can check this by running:

python --version
pip --version

If not installed, download Python from the official website. Once Python is set up, install the necessary libraries:

pip install requests beautifulsoup4

Step 2: Fetch the Booking.com Page

Use the requests library to fetch the HTML content of the Booking.com page:

import requests

url = 'https://www.booking.com/searchresults.html?city=-1456928&checkin_month=12&checkin_monthday=25&checkin_year=2026&checkout_month=12&checkout_monthday=26&checkout_year=2026&group_adults=2'
response = requests.get(url)

if response.status_code == 200:
    html_content = response.text
else:
    print('Failed to retrieve the webpage')

This code attempts to access a search results page for hotels in a specific city. Update the URL to your desired search parameters.

Step 3: Parse HTML with BeautifulSoup

Next, use BeautifulSoup to parse the HTML content and locate the price element. The snippet you provided indicates that the price is nested within specific classes:

from bs4 import BeautifulSoup

soup = BeautifulSoup(html_content, 'html.parser')
price_tag = soup.find('div', class_='bui-price-display__value')

if price_tag:
    price = price_tag.get_text(strip=True)
    print(f'The price of the first hotel is: {price}')
else:
    print('Price information not found')

This script looks for the price within the specified class and prints it. Ensure the class names match the current HTML structure of the page, as websites often update their layouts.

Step 4: Handle Dynamic Content

Booking.com, like many modern websites, uses JavaScript to load content dynamically. If the above method doesn't work, consider using a tool like Selenium to render the page:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

options = webdriver.ChromeOptions()
options.add_argument('--headless')

with webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options) as driver:
    driver.get(url)
    soup = BeautifulSoup(driver.page_source, 'html.parser')
    price_tag = soup.find('div', class_='bui-price-display__value')
    if price_tag:
        price = price_tag.get_text(strip=True)
        print(f'The price of the first hotel is: {price}')
    else:
        print('Price information not found')

This code uses Selenium to simulate a browser session, allowing JavaScript to execute and populate the page content.

Common Errors/Troubleshooting

  • 403 Forbidden: Ensure your headers mimic a real browser to avoid being blocked.
  • Element Not Found: Check if the class names have changed; inspect the live page.
  • Dynamic Content Not Loaded: Use Selenium to handle JavaScript-rendered content.

Conclusion

Using Python and BeautifulSoup, you've learned how to scrape hotel prices from Booking.com. This guide provided you with the foundational knowledge to expand your web scraping skills further. Remember to always consider the ethical implications and terms of service of the websites you scrape.

Frequently Asked Questions

Web scraping legality depends on the website's terms of service and the data being accessed. Always review legal guidelines and permissions.

How can I avoid getting blocked by a website?

Mimic human browsing behavior by setting appropriate headers and avoiding excessive request rates.

What if the website changes its structure?

Regularly update your scraping script to match the current HTML structure of the website.

Can I scrape data from any website?

Not all websites allow scraping. Check the site's terms of service and obtain permission if required.

Why use Selenium for web scraping?

Selenium can render JavaScript, enabling the scraping of dynamic content not initially present in the HTML source.