Nov-10-2019, 10:44 AM
(This post was last modified: Nov-10-2019, 02:41 PM by Gribouillis.)
HI all i am trying to insert data from a python script i have and i am getting an error, i thank you for your help in advance and feel free to let me know if there is a better way to do what I am trying to do here , as i am still new to coding with python any help would be good, i will be adding other locations soon as please letme know if that would be best to do each with another file or all in the one
import mysql.connector
from requests_html import HTMLSession
# create an HTML Session object
session = HTMLSession()
# Use the object above to connect to needed webpage
resp = session.get("https://www.sydneyairport.com.au/flights/?query=&flightType=departure&terminalType=domestic&date=2019-11-05&sortColumn=scheduled_time&ascending=true&showAll=false")
# Run JavaScript code on webpage
resp.html.render()
# parse <span class="with-image"> elements containing airline names
airline_list = []
airline_spans = resp.html.find('.with-image')
for span in airline_spans:
airline_list.append(span.text)
# parse <div class="city-name"> elements containing airline names
dest_list = []
dest_divs = resp.html.find('.city-name')
for span in dest_divs:
dest_list.append(span.text)
# parse <div class="flight-numbers"> elements containing Flight numbers
flightnumber_list = []
flightnumber_divs = resp.html.find('.flight-numbers')
for span in flightnumber_divs:
flightnumber_list.append(span.text)
# parse <div class="heading-large"> elements containing Sec depart time
secdepartime_list = []
secdepartime_divs = resp.html.find('.large-scheduled-time')
for span in secdepartime_divs:
secdepartime_list.append(span.text)
# parse <div class="estimated-time"> elements containing estimated-time
estimatedtime_list = []
estimatedtime_divs = resp.html.find('.estimated-time')
for span in estimatedtime_divs:
estimatedtime_list.append(span.text)
# parse <div class="estimated-time"> elements containing latesttime
status_list = []
status_divs = resp.html.find('.status-container')
for span in status_divs:
status_list.append(span.text)
#Connecting go the mysql database to store the infomation
mydb = mysql.connector.connect(
host="localhost",
user="root",
passwd="*******",
database="FlightData"
)
print("Adding Sydney Domestic Airport Flight Information to the Database")
mycursor = mydb.cursor()
sql = "INSERT INTO FlightData (Origan, destination,Flight-Number, Airline, Scheduled,Estimated,Status ) VALUES (%s, %s)"
val = ("Sydney", dest_list, flightnumber_list, airline_list, secdepartime_list, estimatedtime_list, status_list)
mycursor.execute(sql, val)
mydb.commit()
print(airline_list)
print(dest_list)
print(flightnumber_list)
print(secdepartime_list)
print(estimatedtime_list)
print(status_list)
