AWeirdDev / flights

Fast, robust Google Flights scraper (API) for Python.
https://pypi.org/project/fast-flights
14 stars 2 forks source link

No results for multi-city #2

Open deepernewbie opened 3 months ago

deepernewbie commented 3 months ago
from fast_flights import FlightData, Passengers, create_filter, get_flights

# Create a new filter
filter = create_filter(
    flight_data=[
        # Include more if it's not a one-way trip
        FlightData(
            date='2024-06-19',  # Date of departure
            from_airport="IST",
            to_airport="LAX"
        ),
        FlightData(
            date='2024-07-09',  # Date of departure
            from_airport="NYC",
            to_airport="IST"
        ),
        # ... include more for round trips and multi-city trips
    ],
    trip="multi-city",  # Trip (round-trip, one-way, multi-city)
    seat="economy",  # Seat (economy, premium-economy, business or first)
    passengers=Passengers(
        adults=1,
        children=0,
        infants_in_seat=0,
        infants_on_lap=0
    ),
)

result = get_flights(filter)

# The price is currently... low/typical/high
print("The price is currently", result.current_price)

# Display the first flight
print(result.flights[0])

returns no flights however if I manually use

https://www.google.com/travel/flights?tfs=GhoSCjIwMjQtMDYtMTlqBRIDSVNUcgUSA0xBWBoaEgoyMDI0LTA3LTA5agUSA05ZQ3IFEgNJU1RCAQFIAZgBAw%3D%3D&hl=en&tfu=EgQIABABIgA

which is the generated link there are flights

I think the problem is results take about 2-3 secs to populate but response.get returns almost immediately without waiting

AWeirdDev commented 3 months ago

Could you please clarify the issue?

AWeirdDev commented 3 months ago

Hi there,

This is due to the EU Consent Request page. (See #1).

As mentioned in the issue by IHannes, they added a cookie and everything seems to be working fine.

We can add the CONSENT cookie and theoretically it should be working as expected.

cookies = { "CONSENT": "YES+" }

# tag: v0.3
get_flights(filter, cookies=cookies)

Refer to stackoverflow.com • bypassing-eu-consent-request for more.

P.S. I'm not aware of the "EU Consent Request" when developing this project since I'm not in the region. I'm deeply sorry for my oversight and any inconvenience caused.

Regards, AWeirdDev

deepernewbie commented 2 months ago

No this is not about the cookies I know about this because I debugged into the return request and get the html text and rendered it in

https://htmledit.squarefree.com/

it renders find but there are no flights and blue color flow indicating search is continuing is running left to right I believe this is about requests.get does not wait for an object to load and returns immediately If I use the url generated by your code on some other google flight scraper that uses selenium It works because they added something like as follows

driver.get(url)
WebDriverWait(driver, timeout=10).until(lambda d: len(_get_flight_elements(d)) > 100)
results = _get_flight_elements(driver)

where

def _get_flight_elements(driver):
    return driver.find_element(by=By.XPATH, value='//body[@id = "yDmH0d"]').text.split('\n')
AWeirdDev commented 2 months ago

Hi again,

Ahh, I understand the context now.

Both round-trip and one-way works fine, but not multi-city, as cited here:

https://github.com/AWeirdDev/flights/blob/c6e4d964e7eee673ae85f1f2ce3a18ff63147ef6/fast_flights/flights.proto#L25

I assume that multi-city loads custom Javascript that cannot be handled by requests. It requires something like puppeteer or playwright.

Just to keep this package light, we can use the try.playwright.tech API to scrape the flights. If you'd like me to implement this, please let me know. Alternatively, you can try the code below before v0.4 rolls out:

API

```python requests.post( "https://try.playwright.tech/service/control/run", headers={ "User-Agent": "... your user agent" }, json={ "code": "# some playwright python code", "language": "python" } ) ```

Sincerely, AWeirdDev

JobeOneKenobi commented 1 month ago

@AWeirdDev dude this project is awesome but what's even more awesome is your readme. Bro you could like write a newsletter love your style, keep up the great work! I'm working on trying to extract prices, did you look into this / make any progress or hit any walls I should know of? No luck yet, found some prices in the html but they are super random and all over a thousand dollars haha.

AWeirdDev commented 1 month ago

Hey @JobeOneKenobi,

Prices are now available on v1.x. For more information, please refer to wiki/What's-New.

P.S. I do put a lot of time into the READMEs, and you're the first to perceive this :skull: Thanks, man!

Cheers

JobeOneKenobi commented 1 month ago

Awesome!

Wait bro are your initials JC? Cuz SAME lol

On Sat, Jul 13, 2024 at 11:31 PM JC @.***> wrote:

Hey @JobeOneKenobi https://github.com/JobeOneKenobi,

Prices are now available on v1.x. For more information, please refer to wiki/What's-New https://github.com/AWeirdDev/flights/wiki/What's-New.

P.S. I do put a lot of time into the READMEs, and you're the first to perceive this 💀 Thanks, man!

Cheers

— Reply to this email directly, view it on GitHub https://github.com/AWeirdDev/flights/issues/2#issuecomment-2227182609, or unsubscribe https://github.com/notifications/unsubscribe-auth/BDGRANGHKDEY6KJRI3HJV23ZMHWKVAVCNFSM6AAAAABIG7AQR2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDEMRXGE4DENRQHE . You are receiving this because you were mentioned.Message ID: @.***>