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

該函數(shù)以目的地、入住和退房日期以及客人數(shù)量作為輸入,并返回包含酒店名稱和價(jià)格的酒店列表。

3. 使用 Flask 構(gòu)建 API

為了使此抓取服務(wù)可用,我們將使用 Flask 構(gòu)建一個(gè) API。該 API 將接受搜索條件作為參數(shù)并返回抓取的數(shù)據(jù)。

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 端點(diǎn) /api/booking-prices 采用目的地、入住和退房日期以及客人數(shù)量的查詢參數(shù),并以 JSON 格式返回酒店價(jià)格。

在免費(fèi)云托管上部署 API

對(duì)于免費(fèi)云托管,我們將使用 Heroku,一個(gè)用于部署 Web 應(yīng)用程序的流行平臺(tái)。

1. 準(zhǔn)備部署

創(chuàng)建具有以下內(nèi)容的 Procfile:

web: python app.py

2.部署到 Heroku

按照以下步驟將 API 部署到 Heroku:

將 your-app-name 替換為您想要的應(yīng)用程序名稱。

結(jié)論

本博客提供了全面的指南,介紹如何使用 Python 從 Booking.com 提取酒店價(jià)格、構(gòu)建 API 來(lái)提供數(shù)據(jù)以及在免費(fèi)云托管平臺(tái)上部署服務(wù)。通過(guò)遵循這些步驟,您可以創(chuàng)建一個(gè)強(qiáng)大的系統(tǒng)來(lái)提取和跟蹤酒店價(jià)格,使您能夠提供有價(jià)值的旅行數(shù)據(jù)情報(bào)和酒店數(shù)據(jù)抓取服務(wù)。

原文鏈接:How to Extract Hotel Prices from Booking.com Using Python?

上一篇:

利用NodeJS、Java、Python無(wú)縫集成人臉檢測(cè)API實(shí)戰(zhàn)指南

下一篇:

在Java, NodeJS, 和Ruby中集成人臉關(guān)鍵點(diǎn)API的實(shí)戰(zhàn)指南
#你可能也喜歡這些API文章!

我們有何不同?

API服務(wù)商零注冊(cè)

多API并行試用

數(shù)據(jù)驅(qū)動(dòng)選型,提升決策效率

查看全部API→
??

熱門場(chǎng)景實(shí)測(cè),選對(duì)API

#AI文本生成大模型API

對(duì)比大模型API的內(nèi)容創(chuàng)意新穎性、情感共鳴力、商業(yè)轉(zhuǎn)化潛力

25個(gè)渠道
一鍵對(duì)比試用API 限時(shí)免費(fèi)

#AI深度推理大模型API

對(duì)比大模型API的邏輯推理準(zhǔn)確性、分析深度、可視化建議合理性

10個(gè)渠道
一鍵對(duì)比試用API 限時(shí)免費(fèi)