开发者社区> 问答> 正文

使用python从JSON刮取Web数据

我想从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)

展开
收起
一码平川MACHEL 2019-02-28 11:35:47 4078 0
1 条回答
写回答
取消 提交回答
  • 您的错误是尝试从响应中解码单个字符:

    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',

    2019-07-17 23:29:40
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载
双剑合璧-Python和大数据计算平台的结合 立即下载