Skip to main content

How To Extract Booking.Com Data For Hotels?

 


This tutorial blog will tell you how to extract booking.com data for hotels with Selectorlib as well as Python. You may also use to scrape hotels data from Booking.com.

How to Extract Booking.com?

Search Booking.com for the Hotels data with conditions like Locations, Room Type, Check In-Check out Date, Total People, etc.

Copy the Search Result URL as well as pass that to the hotel scraper.

With the scraper, we would download the URL with Python Requests.

After that, we will parse the HTML with Selectorlib Template for scraping fields like Location, Name, Room Types, etc.

Then the scraper will save data into the CSV file.

The hotel scraper will scrape the following data. You can add additional fields also:

  • Hotel’s Name
  • Location
  • Room Type
  • Pricing
  • Pricing For (eg: 2 Adults, 1 Night)
  • Overall Ratings
  • Bed Type
  • Total Reviews
  • Rating Tile
  • Links

Installing the Packages Required to Run a Booking Data Scraper

We would require these Packages of Python 3

  • Python Requests to do requests as well as downloading HTML content through Search Result pages from Booking.com. SelectorLib Python suites to extract data with YAML files that we have made from webpages, which we download.

Make installation using pip3

pip3 install requests selectorlib

The Code

It’s time to make a project folder named booking-hotel-scraper. In this folder, add one Python file named scrape.py

After that, paste the code given here in scrape.py

from selectorlib import Extractor
import requests 
from time import sleep
import csv
# Create an Extractor by reading from the YAML file
e = Extractor.from_yaml_file('booking.yml')
def scrape(url):    
headers = {
'Connection': 'keep-alive',
'Pragma': 'no-cache',
'Cache-Control': 'no-cache',
'DNT': '1',
'Upgrade-Insecure-Requests': '1',
# You may want to change the user agent if you get blocked
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.113 Safari/537.36',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'Referer': 'https://www.booking.com/index.en-gb.html',
'Accept-Language': 'en-GB,en-US;q=0.9,en;q=0.8',
}
# Download the page using requests
print("Downloading %s"%url)
r = requests.get(url, headers=headers)
# Pass the HTML of the page and create 
return e.extract(r.text,base_url=url)
with open("urls.txt",'r') as urllist, open('data.csv','w') as outfile:
fieldnames = [
"name",
"location",
"price",
"price_for",
"room_type",
"beds",
"rating",
"rating_title",
"number_of_ratings",
"url"
]
writer = csv.DictWriter(outfile, fieldnames=fieldnames,quoting=csv.QUOTE_ALL)
writer.writeheader()
for url in urllist.readlines():
data = scrape(url) 
if data:
for h in data['hotels']:
writer.writerow(h)
# sleep(5)

This code will:

Open the file named urls.txt as well as download HTML content given for every link in that.

Parse this HTML with Selectorlib Template named booking.yml

Then save the output file in the CSV file named data.csv

It’s time to make a file called urls.txt as well as paste the search result URLs in it. Then we need to create a Selectorlib Template.

Make Selectorlib Template for Scraping Hotels Data from Booking.com Searching Results

You may notice that within a code given above, which we used the file named booking.yml. The file makes this code so short and easy. The magic after making this file is the Web Scraping tool called Selectorlib.

Selectorlib makes selecting, marking, as well as extracting data from the webpages visually easy. A Selectorlib Web Scraping Chrome Extension allows you to mark data, which you want to scrape, and makes CSS Selectors required for extracting the data. After that, preview how the data could look like.

In case, you require data that we have given above, you should not use Selectorlib. As we have already done it for you as well as produced an easy “template”, which you may use. Although, if you need to add new fields, you may use Selectorlib for adding those fields into a template.

Let’s see how we have moticed the data fields we needed to extract with Chrome Extension of Selectorlib.

When you have made the template, just click on ‘Highlight’ button to highlight and preview all selectors. In the end, just click on ‘Export’ option and download YAML file, which is a booking.yml file.

Let’s see how the template – booking.yml will look like:

hotels:
css: div.sr_item
multiple: true
type: Text
children:
name:
css: span.sr-hotel__name
type: Text
location:
css: a.bui-link
type: Text
price:
css: div.bui-price-display__value
type: Text
price_for:
css: div.bui-price-display__label
type: Text
room_type:
css: strong
type: Text
beds:
css: div.c-beds-configuration
type: Text
rating:
css: div.bui-review-score__badge
type: Text
rating_title:
css: div.bui-review-score__title
type: Text
number_of_ratings:
css: div.bui-review-score__text
type: Text
url:
css: a.hotel_name_link
type: Link

Run a Web Scraper

For running the web scraper, from a project folder,

  1. Try to search Booking.com to see your Hotels requirements
  2. Copy as well as add search results URLs into urls.txt
  3. Then Run the script python3 scrape.py
  4. Find data from the data.csv file

Let’s take a sample data from the search results pages.

Contact Web Screen Scraping if you want to Extract Booking.com Data for Hotels!

Know More : https://www.webscreenscraping.com/how-to-extract-booking.com-data-for-hotels.php

Comments

Popular posts from this blog

How to Scrape Glassdoor Job Data using Python & LXML?

  This Blog is related to scraping data of job listing based on location & specific job names. You can extract the job ratings, estimated salary, or go a bit more and extract the jobs established on the number of miles from a specific city. With extraction Glassdoor job, you can discover job lists over an assured time, and identify job placements that are removed &listed to inquire about the job that is in trend. In this blog, we will extract Glassdoor.com, one of the quickest expanding job hiring sites. The extractor will scrape the information of fields for a specific job title in a given location. Below is the listing of Data Fields that we scrape from Glassdoor: Name of Jobs Company Name State (Province) City Salary URL of Jobs Expected Salary Client’s Ratings Company Revenue Company Website Founded Years Industry Company Locations Date of Posted Scraping Logics First, you need to develop the URL to find outcomes from Glassdoor. Meanwhile, we will be scraping lists by j...

How To Use Python To Scrape IMDB Movie Data From The Web ?

  We all are always eager to know the best movie or the best comedy show of all time. For all such confusions, reviews, ratings, and people all over the world utilize IMDB, an online library of such material, for trivia linked to the world of movies and television. While people add the information, the database is owned and administered by an Amazon subsidiary. It began as a database in 1990 and was converted to the web in 1993. While anybody can examine the material on the website, if you want to make changes to the facts or add reviews, you must first register. In this blog, we'll look at how to use  Python  to scrape IMDB movie data from the web. IMDB allows users to give ratings to movies and small screen shows, and these ratings have provided the basis of several lists used by movie fans and many others to establish a personal hit list. While IMDB doesn't give an API for querying its data, it does provide a textual download option. A DIY code can also be used to scra...

How Social Media Marketing Company Uses Web Scraping Services?

  Home   Company   Services   Industries   Blog   Contact Us How Social Media Marketing Company Uses Web Scraping Services? Home   How Social Media Marketing Company Uses Web Scraping Services? JUNE 03, 2022 In last several decades, the globe has witnessed the transformation of technology and the beginning of the new digital era. In recent years, enormous transformations and changes have found a significant impact on people's lives and society. The internet has fundamentally transformed the economics that drives the society, in addition to other social advancements. The economy has been impacted by the Internet. Due to advancement in technology every sector has influenced. Web Scraping The change in the technology have developed many untapped sectors. Web scraping also known as data crawling is one of the most prominent aspects of the twenty-first century's new technology. Web scraping is a way of extracting data from the internet and saving it in an o...