开发者社区> 问答> 正文

使用python从JSON刮取Web数据

python小能手 2019-02-28 11:35:47 529

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

分享到
取消 提交回答
全部回答(1)
  • python小能手
    2019-07-17 23:29:40

    您的错误是尝试从响应中解码单个字符:

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

    0 0

集结各类场景实战经验,助你开发运维畅行无忧

推荐文章
相似问题