我正在使用来自 RapidAPI 的 Booking.com API。我想获得酒店的评论,但我不知道如何找到特定酒店的 hotel_id(必填参数)?
import requests
url = "https://booking-com.p.rapidapi.com/v1/hotels/reviews"
querystring = {"hotel_id":"1676161","locale":"en-gb","sort_type":"SORT_MOST_RELEVANT","customer_type":"solo_traveller,review_category_group_of_friends","language_filter":"en-gb,de,fr"}
headers = {
'x-rapidapi-host': "booking-com.p.rapidapi.com",
'x-rapidapi-key': "1bed4aac31mshcd4d969233d7d7dp129f0bjsn58eea903e6c2"
}
response = requests.request("GET", url, headers=headers, params=querystring)
print(response.text)
您可以使用下面的RapidAPI找到hotel_id。
通过过滤器获取可用酒店。指明destination_id 和dest_type -> 使用@Search 位置端点、入住和退房日期、成人和儿童人数。对于可能的过滤器,请使用@Filters进行搜索
import requests
url = "https://booking-com.p.rapidapi.com/v1/hotels/search"
querystring = {"room_number":"1","order_by":"popularity","filter_by_currency":"AED","checkout_date":"2022-07-02","checkin_date":"2022-07-01","units":"metric","adults_number":"2","dest_id":"-553173","dest_type":"city","locale":"en-gb","children_number":"2","children_ages":"5,0","page_number":"0","include_adjacency":"true","categories_filter_ids":"class::2,class::4,free_cancellation::1"}
headers = {
'x-rapidapi-host': "booking-com.p.rapidapi.com",
'x-rapidapi-key': "0195db92d0msh8afac0130adb2c4p1087e9jsn2aeab9e0aa0f"
}
response = requests.request("GET", url, headers=headers, params=querystring)
print(response.text)
结果如下。
"result": [
{
"id": "property_card_791664",
"hotel_name": "Cheval Thorney Court at Hyde Park",
"class_is_estimated": 0,
"hotel_has_vb_boost": 0,
"district": "Kensington and Chelsea",
"cant_book": null,
"distance_to_cc": "4.00",
"mobile_discount_percentage": 0,
"is_mobile_deal": 0,
"is_free_cancellable": 0,
"countrycode": "gb",
"latitude": 51.5011118011927,
"districts": "1545,29,44,333,2280",
"city_name_en": "London",
"min_total_price": 379.24,
"hotel_id": 791664,
...
可以从结果中获取hotel_id。
访问 https://rapidapi.com/tipsters/api/booking-com/tutorials/booking-api-tutorial-1
谢谢。
我给你两种方法:一种是手动,另一种是使用Python代码。理解第一种方法将帮助您了解第二种方法的工作原理。
document.querySelector('input[name="hotel_id"]').value
此命令查找
input
属性设置为 name
的 hotel_id
元素,并检索其 value
,它对应于酒店的 ID。
确保您的 Python 环境中安装了
requests
和 beautifulsoup4
。如果没有,您可以使用 pip 安装它们:
pip install requests beautifulsoup4
import requests
from bs4 import BeautifulSoup
# Replace 'YOUR_HOTEL_PAGE_URL' with the actual URL of the hotel page on booking.com
url = 'YOUR_HOTEL_PAGE_URL'
# Fetch the content of the hotel page
response = requests.get(url)
# Parse the HTML content of the page
soup = BeautifulSoup(response.text, 'html.parser')
# Find the input element with name='hotel_id' and get its value
hotel_id = soup.find('input', {'name': 'hotel_id'})['value']
print(f'The hotel_id is: {hotel_id}')