
2024年免費的文本工具API接口清單
from bs4 import Beautifulsoup
def get hotel prices(destination, check in, check out, guests):
url =f"https://www.booking.com/searchresults.html?ss={destination}&checkin year month
monthday={check in}&checkout year month monthday={check out}&group adults={guests}&no rooms.
response=requests.get(url)
soup=BeautifulSoup(response.text,"html.parser')
hotels =[]
for hotel in soup.find all('div',class_='sr _property block main row'):
name = hotel.find('span',class ='sr-hotel name').text.strip()
price =hotel.find('div',class ='bui-price-display value').text.strip()
hotels.append({'name': name,'price": price})
return hotels
該函數以目的地、入住和退房日期以及客人數量作為輸入,并返回包含酒店名稱和價格的酒店列表。
為了使此抓取服務可用,我們將使用 Flask 構建一個 API。該 API 將接受搜索條件作為參數并返回抓取的數據。
from flask import Flask, request, jsonify
app=Flask(__name__)
@app.route('/api/booking-prices', methods-['GET'])
def booking_prices():
destination=request.args.get('destination')
check in=request.args.get('check in')
check out=request.args.get('check out')
guests =request.args.get("guests')
if not all([destination, check in, check out, guests]):
return jsonify({'error':'please provide all required parameters'}),400
prices =get hotel prices(destination, check in, check out, guests)
return jsonify(prices)
if __name__=='__main__':
app.run(debug=True)
此 API 端點 /api/booking-prices 采用目的地、入住和退房日期以及客人數量的查詢參數,并以 JSON 格式返回酒店價格。
對于免費云托管,我們將使用 Heroku,一個用于部署 Web 應用程序的流行平臺。
創建具有以下內容的 Procfile:
web: python app.py
按照以下步驟將 API 部署到 Heroku:
將 your-app-name 替換為您想要的應用程序名稱。
本博客提供了全面的指南,介紹如何使用 Python 從 Booking.com 提取酒店價格、構建 API 來提供數據以及在免費云托管平臺上部署服務。通過遵循這些步驟,您可以創建一個強大的系統來提取和跟蹤酒店價格,使您能夠提供有價值的旅行數據情報和酒店數據抓取服務。
原文鏈接:How to Extract Hotel Prices from Booking.com Using Python?