我正在获取回溯回溯(最近一次通话最近):

时间:2019-11-16 01:26:05

标签: python json

您好,我几乎已经完成了我的应用,当我以为我希望它能够正常工作的那一刻,我遇到了以下错误:

  

回溯(最近一次通话最近):文件“ Sydneyimport.py”,第10行,在response_data ['response_data']中的for元素中:KeyError:'response_data'–    错误回溯

我已经做过一些研究,但是我的代码找不到问题。我是新来的,所以可能有点小。

import urllib.parse
import requests

main_api = 'https://www.sydneyairport.com.au/_a/flights/?query=&flightType=departure&terminalType=domestic&date=2019-11-10&sortColumn=scheduled_time&ascending=true&showAll=true'

address = 'lhr'
url = main_api + urllib.parse.urlencode({address: address})

response_data = requests.get(url).json()
for element in response_data['response_data']:
    id = element['id']
    airline = element['airline']
    destination = element['destinations']
    flightNumbers = element['flightNumbers']
    scheduledTime = element['scheduledTime']
    estimatedTime = element['estimatedTime']
    scheduledDate = element['scheduledDate']
    latestTime = element['latestTime']
    status = element['status']
    statusColor = element['statusColor']

print (airline, destinations, flightNumbers)

1 个答案:

答案 0 :(得分:1)

如果您将检查响应,则说明您在错误的位置。您正在寻找的数据进入flightData中。我已经更新了代码以检查flightData内部并进行打印。希望这会有所帮助。

import urllib.parse
import requests

main_api = 'https://www.sydneyairport.com.au/_a/flights/?query=&flightType=departure&terminalType=domestic&date=2019-11-10&sortColumn=scheduled_time&ascending=true&showAll=true'

address = 'lhr'
url = main_api + urllib.parse.urlencode({address: address})

response_data = requests.get(url).json()
for element in response_data['flightData']:
    flight_id = element['id']
    airline = element['airline']
    destination = element['destinations']
    flightNumbers = element['flightNumbers']
    scheduledTime = element['scheduledTime']
    estimatedTime = element['estimatedTime']
    scheduledDate = element['scheduledDate']
    latestTime = element['latestTime']
    status = element['status']
    statusColor = element['statusColor']
    print(airline, destination, flightNumbers)