mysql - 我只将一条记录插入到 mysql 数据库中
问题描述
我已经让我的代码工作了,但是由于某种原因,当我打印数据时,我只将一条记录插入到数据库中,我看到了所有正确的数据,但它只是没有进入数据库。我试图更改代码的不同部分,但没有任何区别,所以不确定我做错了什么,提前谢谢你
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()
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:
flightno, From, to, scheduled, estimated, gate = flight
status = 'IDEL'
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
gate = 'IDEL'
status = 'IDEL'
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 ')
sql = "INSERT INTO flightinfo (origin, airline, destinations, flightNumbers, scheduledTime, estimatedTime, status) VALUES (str(From), str(to), str(flightno), str(scheduled), str(estimated), str(status), str(gate)"
val = (str(From), str(to), str(flightno), str(scheduled), str(estimated), str(status), str(gate))
#data.append(val)
print (val)
# doing a batch insert
#mycursor.executemany(sql, val)
mycursor.executemany(sql,())
mydb.commit()
print(mycursor.rowcount, "was inserted.")
解决方案
进行这项工作的最小修改如下:
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()
airline_spans = resp.html.find('.SearchResultFlightListRow')
print (airline_spans)
airline_list = [span.text.split('\n') for span in airline_spans]
data = []
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:
flightno, From, to, scheduled, estimated, gate = flight
status = 'IDEL'
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
gate = 'IDEL'
status = 'IDEL'
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 ')
val = (str(From), str(to), str(flightno), str(scheduled), str(estimated), str(status), str(gate))
data.append(val)
sql = "INSERT INTO flightinfo (origin, airline, destinations, flightNumbers, scheduledTime, estimatedTime, status) VALUES (%s, %s, %s, %s, %s, %s, %s)"
# doing a batch insert
mycursor.executemany(sql, data)
mydb.commit()
print(mycursor.rowcount, "was inserted.")
推荐阅读
- visual-studio-code - VS Code 向上/向下滚动移动光标
- cobol - 使用 cobol 和循环进行加法
- python - 如何正确替换符号化表达式中的符号?
- python - 计算时间序列中同一个月之间的平均值
- sql - 创建表但在字段定义中出现语法错误
- jetbrains-ide - JetBrains Rider - “到处搜索”命令与 Shift+Shift 触发的命令不同
- node.js - npm init 脚本不运行。如何解决?
- r - 给定不同的开始和结束日期,找出每个变量的每日平均值
- go - go-pg got 没有关系 认为我的桌子上有关系
- java - 在 for 循环中使用时无法从 Retrofit 获取数据