我想从API中删除时间表数据。返回的数据采用JSON格式。我正在使用python。
我试过以下代码:
snav_timetable_url = "https://booking.snav.it/api/v1/rates/1040/2019-02-25/1042/2019-02-25?lang=1"
fh = urllib.request.urlopen(snav_timetable_url)
snav_timetable = fh.read().decode('utf-8')
fh.close()
snav_timetable_data = json.loads(snav_timetable[len(snav_timetable)-2])
snav_timetable_data_cleaned = []
for departure in snav_timetable_data 'data':
snav_timetable_data_cleaned.append({
'COMPANY': 'Snav',
'CODICE CORSA': departure['coditinera'],
'DEPARTURE DATE TIME': departure['strDatapart'],
'ARRIVAL DATE TIME': departure['strDatarri']
})
但得到错误
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
您的错误是尝试从响应中解码单个字符:
snav_timetable[len(snav_timetable)-2]
'}'
单个字符不是JSON文档,因此正确解析失败。您想要解码整个响应:
snav_timetable_data = json.loads(snav_timetable)
您可能希望在此处切换到使用requests库,它会从API中加载JSON响应:
import requests
snav_timetable_data = requests.get(snav_timetable_url).json()
snav_timetable_data_cleaned = []
for departure in snav_timetable_data'data':
snav_timetable_data_cleaned.append({
'COMPANY': 'Snav',
'CODICE CORSA': departure['coditinera'],
'DEPARTURE DATE TIME': departure['strDatapart'],
'ARRIVAL DATE TIME': departure['strDataarri']
})
请注意,我必须更正到达日期时间参考中的拼写错误; 是的strDataarri,不是strDatarri。
你拼错了一个strDataarri。
此外,请求库可能会使这看起来更清洁。
import requests
snav_timetable_url = "https://booking.snav.it/api/v1/rates/1040/2019-02-25/1042/2019-02-25?lang=1"
r = requests.get(snav_timetable_url).json()
snav_timetable_data_cleaned = []
for departure in r'data':
snav_timetable_data_cleaned.append({
'COMPANY': 'Snav',
'CODICE CORSA': departure['coditinera'],
'DEPARTURE DATE TIME': departure['strDatapart'],
'ARRIVAL DATE TIME': departure['strDataarri']
})
输出:
[{'COMPANY': 'Snav',
'CODICE CORSA': 'NABECASA',
'DEPARTURE DATE TIME': '2019-02-25 08:25',
'ARRIVAL DATE TIME': '2019-02-25 09:20'},
{'COMPANY': 'Snav',
'CODICE CORSA': 'NABECASA',
'DEPARTURE DATE TIME': '2019-02-25 12:30',
'ARRIVAL DATE TIME': '2019-02-25 13:25'},
{'COMPANY': 'Snav',
'CODICE CORSA': 'NABECASA',
'DEPARTURE DATE TIME': '2019-02-25 16:20',
'ARRIVAL DATE TIME': '2019-02-25 17:15'},
{'COMPANY': 'Snav',
'CODICE CORSA': 'NABECASA',
'DEPARTURE DATE TIME': '2019-02-25 19:00',
'ARRIVAL DATE TIME': '2019-02-25 19:55'},
{'COMPANY': 'Snav',
'CODICE CORSA': 'NABECASA',
'DEPARTURE DATE TIME': '2019-02-26 08:25',
'ARRIVAL DATE TIME': '2019-02-26 09:20'},
{'COMPANY': 'Snav',
'CODICE CORSA': 'NABECASA',
'DEPARTURE DATE TIME': '2019-02-26 12:30',
'ARRIVAL DATE TIME': '2019-02-26 13:25'},
{'COMPANY': 'Snav',
'CODICE CORSA': 'NABECASA',
'DEPARTURE DATE TIME': '2019-02-26 16:20',
'ARRIVAL DATE TIME': '2019-02-26 17:15'},
{'COMPANY': 'Snav',
'CODICE CORSA': 'NABECASA',
'DEPARTURE DATE TIME': '2019-02-26 19:00',
'ARRIVAL DATE TIME': '2019-02-26 19:55'},
{'COMPANY': 'Snav',
'CODICE CORSA': 'NABECASA',
'DEPARTURE DATE TIME': '2019-02-27 08:25',
'ARRIVAL DATE TIME': '2019-02-27 09:20'},
{'COMPANY': 'Snav',
'CODICE CORSA': 'NABECASA',
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。