📜  Python|使用酒店价格比较 API 查找酒店价格

📅  最后修改于: 2022-05-13 01:55:33.204000             🧑  作者: Mango

Python|使用酒店价格比较 API 查找酒店价格

Makcorps hotel API用于获取 JSON 数据,比较来自 200 多个网站的酒店价格、评级和评论,包括: Agoda.com、Hotels.com、Expedia 等。它是围绕 GET 请求组织的。可以免费使用此 API 获取任何酒店或任何城市的价格、评级、评论、历史价格和许多其他信息。

要使用此 API,必须需要 API 密钥,可在此处获取表单

注意:用户需要在makcorps.com上创建一个帐户,然后才能使用 API。

需要的模块:

要求

pip install requests

执行

此 API 将根据城市名称进行搜索。假设如果搜索伦敦城市,则输出 JSON 将是该城市中不同供应商的不同酒店的价格比较以及它们的名称。根据供应商的价格,最好的价格也会显示出来。

下面是实现:

# Python program to find live hotel prices  
# Python program to find live hotel prices
# status using Makcorps Hotel API
  
# import required modules
import requests, json
  
# base_url variable to store url
base_url = "https://api.makcorps.com/free/"
  
# enter city name here
city = "london"
  
# complete_url variable to
# store complete url address
complete_url = base_url + city
  
# Declaring headers needed
headers = {
    'Authorization': 'JWT your_API_id',
}
  
# get method of requests module
# return response object
response_ob = requests.get(complete_url, headers=headers)
  
# json method of response object convert
# json format data into python format data
result = response_ob.json()
  
# Now check the value of status_code is equal
# to "200" or not, if equal that means record is
# found otherwise record is not found
if response_ob.status_code == 200:
  
    #  name is extracting from
    # the result variable data
    print("price comparison data for a random date of city london is:")
    print(result)
else:
    print("record is not found for given request")

输出:

price comparison data for a random date of city london is:
{'comparison':
  [
    {
     'vendor1-price': 'US$217',
     'vendor3-price': 'US$246',
     'vendor2-price': 'US$217',
     'vendor3': 'travelup.com',
     'vendor1': 'Travelocity',
     'Hotel': 'Park Plaza Westminster Bridge London',
     'Best-price': 'US$\xa0246US$\xa0215Booking.com',
     'vendor2': 'Orbitz.com'
    }, 

    {
     'vendor1-price': '',
     'vendor3-price': '',
     'vendor2-price': '',
     'vendor3': 'Travelocity',
     'vendor1': 'Expedia.com',
     'Hotel': 'Travelodge London Covent Garden',
     'Best-price': 'US$\xa074Travelodge',
     'vendor2': 'Hotels.com'
    }, 

    {
     'vendor1-price': 'US$167',
     'vendor3-price': 'US$183',
     'vendor2-price': 'US$171',
     'vendor3': 'Nustay.com', 
     'vendor1': 'ParkGrandLondon',
     'Hotel': 'Park Grand London Kensington',
     'Best-price': 'US$\xa0170Booking.com', 
     'vendor2': 'Travelocity'
    }, 

    {
     'vendor1-price': '',
     'vendor3-price': '',
     'vendor2-price': '',
     'vendor3': 'Orbitz.com',
     'vendor1': 'Expedia.com',
     'Hotel': 'Travelodge London City hotel',
     'Best-price': 'US$\xa056Travelodge',
     'vendor2': 'Nustay.com'
    }, 

    {
     'vendor1-price': 'US$205',
     'vendor3-price': 'US$232',
     'vendor2-price': 'US$185',
     'vendor3': 'ZenHotels.com', 
     'vendor1': 'Booking.com',
     'Hotel': 'The Tower Hotel',
     'Best-price': 'US$\xa0206Orbitz.com',
     'vendor2': 'Trip.com'
    },

    {
     'vendor1-price': 'US$77',
     'vendor3-price': 'US$94',
     'vendor2-price': 'US$87',
     'vendor3': 'Nustay.com', 
     'vendor1': 'Official Site',
     'Hotel': 'Point A Hotel, London Kings Cross St Pancras',
     'Best-price': 'US$\xa087Orbitz.com',
     'vendor2': 'Booking.com'
    },

    {
     'vendor1-price': 'US$224',
     'vendor3-price': 'US$242',
     'vendor2-price': 'US$217',
     'vendor3': 'travelup.com', 
     'vendor1': 'Orbitz.com',
     'Hotel': 'Strand Palace Hotel',
     'Best-price': 'US$\xa0223Booking.com',
     'vendor2': 'ZenHotels.com'
    }
  ]
}