You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何结合originRadius和destinationRadius参数,用Python调用Amadeus API查询航班?

在Amadeus API中添加originRadius和destinationRadius参数实现航班查询

originRadius和destinationRadius参数用于扩大航班搜索范围,允许你指定出发地/目的地机场周边的搜索半径(单位:公里),API会返回该范围内所有符合条件的机场的航班结果。

修改代码步骤:

  • 给check_flights方法新增两个可选参数(可设置默认值,避免调用时必须传参)
  • 在API请求中按需加入这两个参数(仅当有值时传递,避免无效参数干扰)

修改后的完整代码:

def check_flights(
    self, 
    originLocationCode, 
    destinationLocationCode, 
    departureDate, 
    returnDate, 
    adults, 
    currencyCode,
    originRadius=None,  # 新增:出发地搜索半径(公里)
    destinationRadius=None  # 新增:目的地搜索半径(公里)
):
    ''' Return a list of FlightData objects based on the API search results. '''
    
    amadeus = Client(client_id=API_KEY, client_secret=API_SECRET)
    
    try:
        # 构建请求参数字典,灵活处理可选参数
        params = {
            'originLocationCode': originLocationCode,
            'destinationLocationCode': destinationLocationCode,
            'departureDate': departureDate,
            'returnDate': returnDate,
            'adults': adults,
            'currencyCode': currencyCode
        }
        # 仅当参数有值时添加到请求中
        if originRadius is not None:
            params['originRadius'] = originRadius
        if destinationRadius is not None:
            params['destinationRadius'] = destinationRadius
            
        response = amadeus.get(API_URL, **params)
        data = response.data

        self.save_data_to_file(data=response.body)

    except ResponseError as error:
        # TO DO: If error occurs, render error in available_flights
        return error

使用示例:

如果要搜索北京(PEK)周边30公里、上海(PVG)周边50公里的往返航班,调用时可这样传参:

check_flights(
    originLocationCode="PEK",
    destinationLocationCode="PVG",
    departureDate="2024-10-01",
    returnDate="2024-10-07",
    adults=2,
    currencyCode="CNY",
    originRadius=30,
    destinationRadius=50
)

内容的提问来源于stack exchange,提问作者cimbaIG

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 05:31:23