
2024年免費(fèi)的文本工具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
該函數(shù)以目的地、入住和退房日期以及客人數(shù)量作為輸入,并返回包含酒店名稱和價(jià)格的酒店列表。
為了使此抓取服務(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à)格。
對(duì)于免費(fèi)云托管,我們將使用 Heroku,一個(gè)用于部署 Web 應(yīng)用程序的流行平臺(tái)。
創(chuàng)建具有以下內(nèi)容的 Procfile:
web: python app.py
按照以下步驟將 API 部署到 Heroku:
將 your-app-name 替換為您想要的應(yīng)用程序名稱。
本博客提供了全面的指南,介紹如何使用 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?
2024年免費(fèi)的文本工具API接口清單
如何找到合適的API
8個(gè)最佳Swagger替代品
探索頂級(jí)PDF水印API:PDFBlocks(2024年更新)
API 安全 101:是什么、如何做、為什么做
探索SOAP API:深入了解其優(yōu)勢(shì)和戰(zhàn)略實(shí)施
一站式教程:使用Python、PHP和Ruby調(diào)用企業(yè)網(wǎng)站備案/ICP域名備案查詢API
開(kāi)放API的優(yōu)勢(shì)、挑戰(zhàn)和戰(zhàn)略見(jiàn)解
探索智能化新境界:云知聲山海大模型API集成與應(yīng)用實(shí)踐
對(duì)比大模型API的內(nèi)容創(chuàng)意新穎性、情感共鳴力、商業(yè)轉(zhuǎn)化潛力
一鍵對(duì)比試用API 限時(shí)免費(fèi)