当前位置: 代码迷 >> python >> 想要从 url 访问特定的字典值并在 python 中获取键 0 错误
  详细解决方案

想要从 url 访问特定的字典值并在 python 中获取键 0 错误

热度:65   发布时间:2023-07-14 08:46:12.0

我想访问在URL发送的字典中的位置值

我写了下面的代码,但它抛出了一个错误键 0。我想从URL访问一些值,如地点、标题、几何图形并将其写入CSV文件。

import urllib2
import json

url = 'https://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&starttime=2016-10-01&endtime=2016-10-02'

# download the json string
json_string = urllib2.urlopen(url).read()

# de-serialize the string so that we can work with it
j = json.loads(json_string)

names = [d['properties'] for d in j[0]['type']]

print names

我是python的新手。

j是一个具有键['type', 'metadata', 'features', 'bbox'] ,但不是 0。您可能正在寻找j['features'] ,而不是j[0]['type'] ,但是names的值是一个字典列表,而不是names 我假设该站点的 JSON API 自从您(或任何编写代码的人)上次使用它以来已经发生了变化。