python error sting not defined

adbyits
adbyits used Ask the Experts™
on
Hi all i am having a issue where the string called airline is comming up and telling me not definned i know it got something to with indents but each time i change this i cant get it to work can someone please tell me how iw fix it




from requests_html import HTMLSession
import mysql.connector

mydb = mysql.connector.connect(
    host="localhost",
    user="root",
    passwd="**",
    database="flightdata"
)

mycursor = mydb.cursor()

# create an HTML Session object
session = HTMLSession()

# Use the object above to connect to needed webpage
resp = session.get(
    "https://www.adelaideairport.com.au/flight-information/flight-search/?flt_no=&carrier=All&city=&dte=Current&leg=Departures")

# Run JavaScript code on webpage
resp.html.render()
data = []

airline_spans = resp.html.find('.SearchResultFlightListRow')
#print (airline_spans)
airline_list = [span.text.split('\n') for span in airline_spans]

for flight in airline_list:
    if len(flight) == 7:
        flightno, From, to, scheduled, estimated, gate, status = flight
     #   print ("This is a " + estimated)
        if estimated == "":
          #  print (" currently no dely ")
        #    print ("This is a " + estimated)
            estimated = 'IDEL'
    #    print (f'Flight no {flightno} from  {From} to {to} is scheduled to depart at {scheduled} from gate {gate} and flight status is {status}')

    elif len(flight) == 6:
   #     print (flight)
        status = "IDEL"
        #print (status)
        flightno, From, to, scheduled, estimated, gate = flight
      #  print ("This is a " + estimated)
        if estimated == "":
        #    print (" currently no dely ")
        #    print ("This is a " + estimated)
            estimated = 'IDEL'
    #    print (f'Flight no {flightno} from  {From} to {to} is scheduled to depart at {scheduled} from gate {gate} ')

    elif len(flight) == 5:

        flightno, From, to, scheduled, estimated = flight

        if 'VA' in flightno:
            airline = ('Virgin Australia')
            print (airline)
   
        if "TT" in flightno:
             airline = ('Tigerair')
   
        if "SQ" in flightno:
            airline = ('Singapore Airlines')
   
        if 'NZ' in flightno:
            airline = ('Air New Zealand')
          #  print  (airline)
   
        if 'QQ' in flightno:
            airline = ('Alliance')
          #  print  (airline)
   
        if "CX" in flightno:
            airline = ('Cathay Pacific')
   
        if "CZ" in flightno:
            airline = ('China Southern Airlines ')
   
        if 'EK' in flightno:
            airline = ('Emirates')
            print  (airline)
   
        if "FJ" in flightno:
             airline = ('Fiji Airways')
   
        if "FP" in flightno:
            airline = ('FlyPelican')


        origin = str(From)
        flight_id = str('na')
        airline = str(airline)
        destination = str(to)
        flightNumbers = str(flightno)
        scheduledTime = str(scheduled)
        estimatedTime = str(estimated)
        scheduledDate = str('na')
        latestTime = str(estimated)
        status = str(status)
        gate = str(gate)
   
        print (airline)

  #  print (From, flight_id, flightNumbers, airline, destination, scheduledTime, scheduledDate, latestTime, status, gate)

    sql = "INSERT INTO flightinfo (origin, id, airline, destinations, flightNumbers, scheduledTime, estimatedTime, scheduledDate, latestTime, status, gate) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"

    val = (
    origin, flight_id, airline, destination, flightNumbers, scheduledTime, estimatedTime, scheduledDate, latestTime,
    status, gate)

    data.append(val)
 #   print(data)
# doing a batch insert
mycursor.executemany(sql, data)

mydb.commit()

print(mycursor.rowcount, "was inserted.")
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
nociSoftware Engineer
Distinguished Expert 2018

Commented:
Please use a CODE block....
from requests_html import HTMLSession
import mysql.connector

mydb = mysql.connector.connect(
    host="localhost",
    user="root",
    passwd="**",
    database="flightdata"
)

mycursor = mydb.cursor()

# create an HTML Session object
session = HTMLSession()

# Use the object above to connect to needed webpage
resp = session.get(
    "https://www.adelaideairport.com.au/flight-information/flight-search/?flt_no=&carrier=All&city=&dte=Current&leg=Departures")

# Run JavaScript code on webpage
resp.html.render()
data = []

airline_spans = resp.html.find('.SearchResultFlightListRow')
#print (airline_spans)
airline_list = [span.text.split('\n') for span in airline_spans]

for flight in airline_list:
    if len(flight) == 7:
        flightno, From, to, scheduled, estimated, gate, status = flight
        #print ("This is a " + estimated)
        if estimated == "":
            #print (" currently no dely ")
            #print ("This is a " + estimated)
            estimated = 'IDEL'
        #print (f'Flight no {flightno} from  {From} to {to} is scheduled to depart at {scheduled} from gate {gate} and flight status is {status}')

    elif len(flight) == 6:
        #print (flight)
        status = "IDEL"
        #print (status)
        flightno, From, to, scheduled, estimated, gate = flight
        #print ("This is a " + estimated)
        if estimated == "":
            #print (" currently no dely ")
            #print ("This is a " + estimated)
            estimated = 'IDEL'
         #print (f'Flight no {flightno} from  {From} to {to} is scheduled to depart at {scheduled} from gate {gate} ')

    elif len(flight) == 5:

        flightno, From, to, scheduled, estimated = flight

        if 'VA' in flightno:
            airline = ('Virgin Australia')
            print (airline)
   
        if "TT" in flightno:
             airline = ('Tigerair')
   
        if "SQ" in flightno:
            airline = ('Singapore Airlines')
   
        if 'NZ' in flightno:
            airline = ('Air New Zealand')
          #  print  (airline)
   
        if 'QQ' in flightno:
            airline = ('Alliance')
          #  print  (airline)
   
        if "CX" in flightno:
            airline = ('Cathay Pacific')
   
        if "CZ" in flightno:
            airline = ('China Southern Airlines ')
   
        if 'EK' in flightno:
            airline = ('Emirates')
            print  (airline)
   
        if "FJ" in flightno:
             airline = ('Fiji Airways')
   
        if "FP" in flightno:
            airline = ('FlyPelican')


        origin = str(From)
        flight_id = str('na')
        airline = str(airline)
        destination = str(to)
        flightNumbers = str(flightno)
        scheduledTime = str(scheduled)
        estimatedTime = str(estimated)
        scheduledDate = str('na')
        latestTime = str(estimated)
        status = str(status)
        gate = str(gate)
   
        print (airline)

    #print (From, flight_id, flightNumbers, airline, destination, scheduledTime, scheduledDate, latestTime, status, gate)

    sql = "INSERT INTO flightinfo (origin, id, airline, destinations, flightNumbers, scheduledTime, estimatedTime, scheduledDate, latestTime, status, gate) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"

    val = (
    origin, flight_id, airline, destination, flightNumbers, scheduledTime, estimatedTime, scheduledDate, latestTime,
    status, gate)

    data.append(val)
    #print(data)
# doing a batch insert
mycursor.executemany(sql, data)

mydb.commit()

print(mycursor.rowcount, "was inserted.")

Open in new window

nociSoftware Engineer
Distinguished Expert 2018

Commented:
The Fix would to add after line 52:

airline = "" 

indented under flightno.

 
   elif len(flight) == 5:

        flightno, From, to, scheduled, estimated = flight
        airline = ""

        if 'VA' in flightno:
            airline = ('Virgin Australia')
            print (airline)

Open in new window


That will make airline at least have a blank value or assign it  ??? with airline = "???"
airline will not be defined if all if block fail in the current code

Author

Commented:
this is th line is fails at

        airline = str(airline)
OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

nociSoftware Engineer
Distinguished Expert 2018

Commented:
Yes...
In you code the ONLY way airline gets a value is if any of the IF statements is true. if ALL are false, airline is not defind.
Lets Assume the flight is XX102 then there is no If statement executed ==> airline is undefined.

My suggested code change is to ALWAYS define airline to be at least an empty string.
Alternatively you can put it to:  
airline = "Unknown"

Open in new window

Author

Commented:
I tried it and  i still get  not definded
Software Engineer
Distinguished Expert 2018
Commented:
Now it does for if len(flight) == 5
The SQL statement will still fail...  as airline does not get defined if len(flight) = 6 or 7 .
There is no description of the current error message. IF the change has been done correctly THEN it cannot be the same error.

Ok, without having enough to give a correct solution, (mainly missing description / specification of what it is meant to do)
going on the errormessage in str(airline) as i did before aparantly is not the only problem....

You will have to de with the following OR give a proper specification of what the code is expected to produce.

from requests_html import HTMLSession
import mysql.connector

mydb = mysql.connector.connect(
    host="localhost",
    user="root",
    passwd="**",
    database="flightdata"
)

mycursor = mydb.cursor()

# create an HTML Session object
session = HTMLSession()

# Use the object above to connect to needed webpage
resp = session.get(
    "https://www.adelaideairport.com.au/flight-information/flight-search/?flt_no=&carrier=All&city=&dte=Current&leg=Departures")

# Run JavaScript code on webpage
resp.html.render()
data = []

airline_spans = resp.html.find('.SearchResultFlightListRow')
#print (airline_spans)
airline_list = [span.text.split('\n') for span in airline_spans]
airline=""

for flight in airline_list:
    if len(flight) == 7:
        flightno, From, to, scheduled, estimated, gate, status = flight
        #print ("This is a " + estimated)
        if estimated == "":
            #print (" currently no dely ")
            #print ("This is a " + estimated)
            estimated = 'IDEL'
        #print (f'Flight no {flightno} from  {From} to {to} is scheduled to depart at {scheduled} from gate {gate} and flight status is {status}')

    elif len(flight) == 6:
        #print (flight)
        status = "IDEL"
        #print (status)
        flightno, From, to, scheduled, estimated, gate = flight
        #print ("This is a " + estimated)
        if estimated == "":
            #print (" currently no dely ")
            #print ("This is a " + estimated)
            estimated = 'IDEL'
         #print (f'Flight no {flightno} from  {From} to {to} is scheduled to depart at {scheduled} from gate {gate} ')

    elif len(flight) == 5:

        flightno, From, to, scheduled, estimated = flight

    if 'VA' in flightno:
        airline = ('Virgin Australia')
        print (airline)
   
    if "TT" in flightno:
         airline = ('Tigerair')

    if "SQ" in flightno:
        airline = ('Singapore Airlines')
   
    if 'NZ' in flightno:
        airline = ('Air New Zealand')
        #  print  (airline)
   
    if 'QQ' in flightno:
        airline = ('Alliance')
        #  print  (airline)
   
    if "CX" in flightno:
        airline = ('Cathay Pacific')
   
    if "CZ" in flightno:
        airline = ('China Southern Airlines ')
   
    if 'EK' in flightno:
        airline = ('Emirates')
        print  (airline)
   
    if "FJ" in flightno:
         airline = ('Fiji Airways')
   
    if "FP" in flightno:
        airline = ('FlyPelican')


    origin = str(From)
    flight_id = str('na')
    airline = str(airline)
    destination = str(to)
    flightNumbers = str(flightno)
    scheduledTime = str(scheduled)
    estimatedTime = str(estimated)
    scheduledDate = str('na')
    latestTime = str(estimated)
    status = str(status)
    gate = str(gate)
   
    print (airline)

    #print (From, flight_id, flightNumbers, airline, destination, scheduledTime, scheduledDate, latestTime, status, gate)

    sql = "INSERT INTO flightinfo (origin, id, airline, destinations, flightNumbers, scheduledTime, estimatedTime, scheduledDate, latestTime, status, gate) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"

    val = (
    origin, flight_id, airline, destination, flightNumbers, scheduledTime, estimatedTime, scheduledDate, latestTime,
    status, gate)

    data.append(val)
    #print(data)
# doing a batch insert
mycursor.executemany(sql, data)

mydb.commit()

print(mycursor.rowcount, "was inserted.")

Open in new window

Author

Commented:
What did you change mate it worked well
nociSoftware Engineer
Distinguished Expert 2018
Commented:
(you can do a diff.... between versions...)

i did unindent 1 level all the IF statements below if len(flight) == 5 and the string assignments.
also i added a blank assignment to airline upfront ALL if statements.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial