fast-flights Python Library

repository·dev·Indexed 22 days ago

https://github.com/aweirddev/flights

A fast, robust, and strongly-typed Google Flights scraper API implemented in Python (version 3.0.2). It allows users to perform flight searches using natural language or programmatic queries via create_query and get_flights. The library supports detailed filtering for flight legs (dates, airports, airlines, layovers) and global search parameters (currency, baggage, price). It includes integrations for Bright Data for IP protection and SearchApi for richer data such as price insights and booking options.

Tokens
5.9K
Snippets
16
Records
38
Agent score
79%

What's inside fast-flights

  1. Quickstart: Perform a basic flight search

    dev

    To perform a flight search, use create_query to define your flight legs, passenger details, and trip type, then pass the resulting query to get_flights.

    from fast_flights import (
        FlightQuery,
        Passengers, 
        create_query, 
        get_flights
    )
    
    query = create_query(
        flights=[
            FlightQuery(
                date="YYYY-MM-DD",   # change the date
                from_airport="MYJ",  # three-letter name
                to_airport="TPE",    # three-letter name
            ),
        ],
        seat="economy",  # business/economy/first/premium-economy
        trip="one-way",  # multi-city/one-way/round-trip
        passengers=Passengers(adults=1),
        language="zh-TW",
    )
    res = get_flights(query)
  2. Get started with fast-flights

    dev

    To perform a flight lookup, use create_query to define your search parameters and get_flights (or the returned result) to retrieve data.

    Key configuration parameters include:

    • flights: A list of FlightQuery objects. For round-trip searches, you must provide at least two FlightQuery objects (one for outbound, one for return).
    • trip: The trip type. Supported values are one-way and round-trip. Note that multi-city is not currently supported.
    • seat: The desired cabin class. Supported values are economy, premium-economy, business, or first.
    • passengers: A Passengers object specifying the count for adults, children, infants_in_seat, and infants_on_lap.
    from fast_flights import FlightQuery, Passengers, ResultList, create_query, get_flights
    
    result: ResultList = create_query(
        flights=[
            FlightQuery(date="2025-01-01", from_airport="TPE", to_airport="MYJ")  # (1)
        ],
        trip="one-way",  # (2)
        seat="economy",  # (3)
        passengers=Passengers(adults=2, children=1, infants_in_seat=0, infants_on_lap=0),  # (4)
    )
    
    print(result)
  3. Create a flight query using create_query()

    dev

    You can programmatically build a flight search query using create_query(). This function accepts a list of FlightQuery objects for flight legs and several global parameters like seat, trip, passengers, and language that apply to the entire search.

    To execute the search, pass the resulting query object to get_flights().

    from fast_flights import (
        FlightQuery,
        Passengers,
        create_query
    )
    
    query = create_query(
        flights=[
            FlightQuery(
                date="2067-06-07",
                from_airport="MYJ",
                to_airport="TPE"
            ),
        ],
        seat="economy",
        trip="one-way",
        passengers=Passengers(adults=1, infants_in_seat=2),
        language="en"
    )
    
    # res = get_flights(query)
  4. Use the fast-flights public API

    dev
    The fast-flights package provides a high-level interface for scraping Google Flights data. The primary entry points for querying and retrieving flight information are get_flights, create_query, and the FlightQuery class. For low-level HTML retrieval, you can use fetch_flights_html.
  5. Configure global search filters

    dev

    The create_query() function supports several parameters that apply to the entire search result rather than individual legs:

    • currency: The currency code for pricing (e.g., "USD").
    • max_price: Maximum price allowed, using the selected currency.
    • carry_on_bags: Number of carry-on bags to include.
    • checked_bags: Number of checked bags to include (includes estimated fees in displayed prices).
    • hide_separate_and_self_transfer: Boolean to hide non-through flights.
    • exclude_basic_economy: Boolean to exclude basic economy fares.
    query = create_query(
        flights=[data],
        currency="USD",
        max_price=1500,
        carry_on_bags=1,
        checked_bags=1,
        hide_separate_and_self_transfer=True,
        exclude_basic_economy=True,
    )
  6. Use DataSourceIntegration to extend get_flights()

    dev
    The get_flights function supports custom data sources via the DataSourceIntegration[T] interface. When you pass an instance of DataSourceIntegration to get_flights, the function bypasses the default HTML fetching and parsing logic and calls integration.fetch(q) directly. This allows you to return custom data structures instead of the standard ResultList.
  7. Use SearchApi integration for richer data

    dev

    For improved consistency and access to richer data (like price insights and booking options), use the SearchApi integration. The returned result will be a SearchApiResult object.

    SearchApiResult available fields:

    • flights
    • cheaper_alternatives
    • price_insights
    • booking_options
    from fast_flights.integrations import SearchApi, SearchApiResult
    
    result: SearchApiResult = get_flights(
        ...,
        integration=SearchApi()
    )
    
    # Access rich data
    print(result.flights)
    print(result.cheaper_alternatives)
    print(result.price_insights)
    print(result.booking_options)
    from fast_flights.integrations import SearchApi, SearchApiResult
    
    result: SearchApiResult = get_flights(
        ...,
        integration=SearchApi()
    )
    
    # rich data!
    result.flights
    result.cheaper_alternatives
    result.price_insights
    result.booking_options
  8. Use Bright Data integration for IP protection

    dev

    To protect your IP during scraping, you can use the BrightData integration by passing it to the integration parameter in get_flights.

    from fast_flights.integrations import BrightData
    
    result = get_flights(
        ..., 
        integration=BrightData(zone="...")
    )
  9. Configure passenger counts with Passengers

    dev

    Use the Passengers class to specify the number of travelers.

    Constraints:

    • The total sum of adults, children, infants_in_seat, and infants_on_lap must not exceed 9.
    • You must have at least one adult for every infant on lap (infants_on_lap).
    passengers = Passengers(
        adults=2,
        children=1,
        infants_in_seat=0,
        infants_on_lap=0
    )
  10. Serialize a query to bytes or string

    dev

    Once a query is created, you can serialize it for transmission or storage using to_bytes() or to_str().

    query: Query = create_query(
        flight_data=[
            FlightQuery(
                date="2025-01-01",
                from_airport="TPE",
                to_airport="MYJ",
            )
        ],
        trip="round-trip",
        passengers=Passengers(adults=2, children=1, infants_in_seat=0, infants_on_lap=0),
        seat="economy",
        max_stops=1,
    )
    
    query.to_bytes()  # Base64-encoded (bytes)
    query.to_str()  # Serialize to string