0

here is the github repo: https://github.com/00-Python/Ryanair-Scraper

scraper.py:

I get the following error when there isn't either a return or departure flight on the specified date:

  File "/home/zero/PycharmProjects/ryanairMonitor/scraper.py", line 124, in <module>
    prices= ryanair.return_flight('2023-05-25', '2023-06-07', 'MAN', 'VLC', '2')
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/zero/PycharmProjects/ryanairMonitor/scraper.py", line 108, in return_flight
    prices = [{'Departure': {'date': departure_date, 'departure_time': departure_flyout_times, 'arrival_time': arrival_flyout_times, 'price': prices[0], }}, {'Return': {'date': return_date, 'departure_time': departure_flyback_times, 'arrival_time': arrival_flyback_times, 'price': prices[1], }}]
                                     ^^^^^^^^^^^^^^
UnboundLocalError: cannot access local variable 'departure_date' where it is not associated with a value

The following if statement from line 50 to 94 should set the missing data to No flights available on Departure\Return date and N/A:


        if len(day) == 1:
            day = soup.find('span',
                                class_='date-item__day-of-month date-item__day-of-month--selected body-xl-lg body-xl-sm').text
            month = soup.find('span',
                                  class_='date-item__month date-item__month--selected body-xl-lg body-xl-sm').text
            months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
            date = f'{day} {months.index(month)+1}'

            if date == flyout:
                return_date = 'No flights available on Return date'
                departure_date = f'{day} {month}'

                # get departure and arrival times
                departure_flyout_times = soup.find_all('span', class_='title-l-lg title-l-sm time__hour')[0].text.replace(' ', '')
                arrival_flyout_times = soup.find_all('span', class_='title-l-lg title-l-sm time__hour')[1].text.replace(' ', '')

                departure_flyback_times = 'N/A'
                arrival_flyback_times = 'N/A'

            elif date == flyback:
                departure_date = 'No flights available on Departure date'
                return_date = f'{day} {month}'

                # get departure and arrival times
                departure_flyout_times = 'N/A'
                arrival_flyout_times = 'N/A'

                departure_flyback_times = soup.find_all('span', class_='title-l-lg title-l-sm time__hour')[0].text.replace(' ', '')
                arrival_flyback_times = soup.find_all('span', class_='title-l-lg title-l-sm time__hour')[1].text.replace(' ', '')
            else:
                day = soup.find_all('span', class_='date-item__day-of-month body-xl-lg body-xl-sm')
        else:
            day = soup.find_all('span',
                                class_='date-item__day-of-month date-item__day-of-month--selected body-xl-lg body-xl-sm')
            month = soup.find_all('span',
                                  class_='date-item__month date-item__month--selected body-xl-lg body-xl-sm')
            departure_date = f'{day[0].text} {month[0].text}'
            return_date = f'{day[1].text} {month[1].text}'

            # get departure and arrival times
            departure_flyout_times = soup.find_all('span', class_='title-l-lg title-l-sm time__hour')[0].text.replace(' ', '')
            arrival_flyout_times = soup.find_all('span', class_='title-l-lg title-l-sm time__hour')[1].text.replace(' ','')

            departure_flyback_times = soup.find_all('span', class_='title-l-lg title-l-sm time__hour')[1].text.replace(' ', '')
            arrival_flyback_times = soup.find_all('span', class_='title-l-lg title-l-sm time__hour')[2].text.replace(' ', '')

Why is the departure_date variable not being created? and how do i fix?

when using the return_flight method, the program should return a list of dictionaries like the following if there is flight data for both the departure and return: [{'Departure': {'date': '25 May', 'departure_time': '05:45', 'arrival_time': '09:20', 'price': '£90.39'}}, {'Return': {'date': '08 Jun', 'departure_time': '09:20', 'arrival_time': '09:55', 'price': '£49.12'}}]

like the following if there isn't a Departure flight on specified date: [{'Departure': {'date': '25 May', 'departure_time': 'N/A', 'arrival_time': 'N/A', 'price': 'N/A'}}, {'Return': {'date': '08 Jun', 'departure_time': '09:20', 'arrival_time': '09:55', 'price': '£49.12'}}]

like this if there isn't a Return Flight on specified date: [{'Departure': {'date': '25 May', 'departure_time': '05:45', 'arrival_time': '09:20', 'price': '£90.39'}}, {'Return': {'date': '08 Jun', 'departure_time': 'N/A', 'arrival_time': 'N/A', 'price': N/A'}}]

baduker
  • 19,152
  • 9
  • 33
  • 56
  • 1
    Does this answer your question? [UnboundLocalError trying to use a variable (supposed to be global) that is (re)assigned (even after first use)](https://stackoverflow.com/questions/370357/unboundlocalerror-trying-to-use-a-variable-supposed-to-be-global-that-is-rea) – baduker Apr 20 '23 at 20:13
  • No, the variable is created inside the method. I think it is something to do with the if statement. Thanks for the help :) – Joseph Webster Apr 20 '23 at 20:56

0 Answers0