我正在尝试使用其API和以下代码从DarkSky检索天气预报。我只对每小时的预测数据感兴趣:
url="https://api.darksky.net/forecast/api_key/33.972386,-84.231986"
response = requests.get(url)
data = response.json()
data
这是我从数据提取中获得的JSON数据的一部分:
{'latitude': 33.972386,
'longitude': -84.231986,
'timezone': 'America/New_York',
'currently': {'time': 1553052005,
'summary': 'Clear',
'icon': 'clear-night',
'nearestStormDistance': 23,
'nearestStormBearing': 169,
'precipIntensity': 0,
'precipProbability': 0,
'temperature': 43.69,
'apparentTemperature': 43.69,
'dewPoint': 25.61,
'humidity': 0.49,
'pressure': 1026.37,
'windSpeed': 1.42,
'windGust': 4.94,
'windBearing': 79,
'cloudCover': 0,
'uvIndex': 0,
'visibility': 3.86,
'ozone': 309.99},
'minutely': {'summary': 'Clear for the hour.',
'icon': 'clear-night',
'data': [{'time': 1553052000, 'precipIntensity': 0, 'precipProbability': 0},
{'time': 1553052060, 'precipIntensity': 0, 'precipProbability': 0},
{'time': 1553052120, 'precipIntensity': 0, 'precipProbability': 0},
{'time': 1553052180, 'precipIntensity': 0, 'precipProbability': 0},
{'time': 1553052240, 'precipIntensity': 0, 'precipProbability': 0},
{'time': 1553052300, 'precipIntensity': 0, 'precipProbability': 0},
{'time': 1553052360, 'precipIntensity': 0, 'precipProbability': 0},
{'time': 1553052420, 'precipIntensity': 0, 'precipProbability': 0},
{'time': 1553052480, 'precipIntensity': 0, 'precipProbability': 0},
{'time': 1553052540, 'precipIntensity': 0, 'precipProbability': 0},
{'time': 1553052600, 'precipIntensity': 0, 'precipProbability': 0},
{'time': 1553052660, 'precipIntensity': 0, 'precipProbability': 0},
{'time': 1553052720, 'precipIntensity': 0, 'precipProbability': 0},
{'time': 1553052780, 'precipIntensity': 0, 'precipProbability': 0},
{'time': 1553052840, 'precipIntensity': 0, 'precipProbability': 0},
{'time': 1553052900, 'precipIntensity': 0, 'precipProbability': 0},
{'time': 1553052960, 'precipIntensity': 0, 'precipProbability': 0},
{'time': 1553053020, 'precipIntensity': 0, 'precipProbability': 0},
{'time': 1553053080, 'precipIntensity': 0, 'precipProbability': 0},
{'time': 1553053140, 'precipIntensity': 0, 'precipProbability': 0},
{'time': 1553053200, 'precipIntensity': 0, 'precipProbability': 0},
{'time': 1553053260, 'precipIntensity': 0, 'precipProbability': 0},
{'time': 1553053320, 'precipIntensity': 0, 'precipProbability': 0},
{'time': 1553053380, 'precipIntensity': 0, 'precipProbability': 0},
{'time': 1553053440, 'precipIntensity': 0, 'precipProbability': 0},
{'time': 1553053500, 'precipIntensity': 0, 'precipProbability': 0},
{'time': 1553053560, 'precipIntensity': 0, 'precipProbability': 0},
{'time': 1553053620, 'precipIntensity': 0, 'precipProbability': 0},
{'time': 1553053680, 'precipIntensity': 0, 'precipProbability': 0},
{'time': 1553053740, 'precipIntensity': 0, 'precipProbability': 0},
{'time': 1553053800, 'precipIntensity': 0, 'precipProbability': 0},
{'time': 1553053860, 'precipIntensity': 0, 'precipProbability': 0},
{'time': 1553053920, 'precipIntensity': 0, 'precipProbability': 0},
{'time': 1553053980, 'precipIntensity': 0, 'precipProbability': 0},
{'time': 1553054040, 'precipIntensity': 0, 'precipProbability': 0},
{'time': 1553054100, 'precipIntensity': 0, 'precipProbability': 0},
{'time': 1553054160, 'precipIntensity': 0, 'precipProbability': 0},
{'time': 1553054220, 'precipIntensity': 0, 'precipProbability': 0},
{'time': 1553054280, 'precipIntensity': 0, 'precipProbability': 0},
{'time': 1553054340, 'precipIntensity': 0, 'precipProbability': 0},
{'time': 1553054400, 'precipIntensity': 0, 'precipProbability': 0},
{'time': 1553054460, 'precipIntensity': 0, 'precipProbability': 0},
{'time': 1553054520, 'precipIntensity': 0, 'precipProbability': 0},
{'time': 1553054580, 'precipIntensity': 0, 'precipProbability': 0},
{'time': 1553054640, 'precipIntensity': 0, 'precipProbability': 0},
{'time': 1553054700, 'precipIntensity': 0, 'precipProbability': 0},
{'time': 1553054760, 'precipIntensity': 0, 'precipProbability': 0},
{'time': 1553054820, 'precipIntensity': 0, 'precipProbability': 0},
{'time': 1553054880, 'precipIntensity': 0, 'precipProbability': 0},
{'time': 1553054940, 'precipIntensity': 0, 'precipProbability': 0},
{'time': 1553055000, 'precipIntensity': 0, 'precipProbability': 0},
{'time': 1553055060, 'precipIntensity': 0, 'precipProbability': 0},
{'time': 1553055120, 'precipIntensity': 0, 'precipProbability': 0},
{'time': 1553055180, 'precipIntensity': 0, 'precipProbability': 0},
{'time': 1553055240, 'precipIntensity': 0, 'precipProbability': 0},
{'time': 1553055300, 'precipIntensity': 0, 'precipProbability': 0},
{'time': 1553055360, 'precipIntensity': 0, 'precipProbability': 0},
{'time': 1553055420, 'precipIntensity': 0, 'precipProbability': 0},
{'time': 1553055480, 'precipIntensity': 0, 'precipProbability': 0},
{'time': 1553055540, 'precipIntensity': 0, 'precipProbability': 0},
{'time': 1553055600, 'precipIntensity': 0, 'precipProbability': 0}]},
'hourly': {'summary': 'Clear throughout the day.',
'icon': 'clear-day',
'data': [{'time': 1553050800,
'summary': 'Clear',
'icon': 'clear-night',
'precipIntensity': 0,
'precipProbability': 0,
'temperature': 44.13,
'apparentTemperature': 44.13,
'dewPoint': 25.62,
'humidity': 0.48,
'pressure': 1026.33,
'windSpeed': 1.24,
'windGust': 4.78,
'windBearing': 93,
'cloudCover': 0,
'uvIndex': 0,
'visibility': 3.49,
'ozone': 310.66},
{'time': 1553054400,
'summary': 'Clear',
'icon': 'clear-night',
'precipIntensity': 0,
'precipProbability': 0,
'temperature': 42.82,
'apparentTemperature': 42.82,
'dewPoint': 25.57,
'humidity': 0.5,
'pressure': 1026.44,
'windSpeed': 1.95,
'windGust': 5.25,
'windBearing': 60,
'cloudCover': 0,
'uvIndex': 0,
'visibility': 4.6,
'ozone': 308.68},
{'time': 1553058000,
'summary': 'Clear',
'icon': 'clear-night',
'precipIntensity': 0,
'precipProbability': 0,
'temperature': 42.45,
'apparentTemperature': 40.02,
'dewPoint': 25.52,
'humidity': 0.51,
'pressure': 1026.39,
'windSpeed': 4.15,
'windGust': 8.98,
'windBearing': 61,
'cloudCover': 0,
'uvIndex': 0,
'visibility': 10,
'ozone': 307.84},
{'time': 1553061600,
'summary': 'Clear',
'icon': 'clear-night',
'precipIntensity': 0,
'precipProbability': 0,
'temperature': 41.97,
'apparentTemperature': 38.42,
'dewPoint': 25.8,
'humidity': 0.52,
'pressure': 1025.98,
'windSpeed': 5.52,
'windGust': 11.44,
'windBearing': 63,
'cloudCover': 0,
'uvIndex': 0,
'visibility': 10,
'ozone': 307.21},
{'time': 1553065200,
'summary': 'Clear',
'icon': 'clear-night',
'precipIntensity': 0,
'precipProbability': 0,
'temperature': 40.7,
'apparentTemperature': 36.97,
'dewPoint': 25.86,
'humidity': 0.55,
'pressure': 1025.88,
'windSpeed': 5.44,
'windGust': 10.93,
'windBearing': 61,
'cloudCover': 0,
'uvIndex': 0,
'visibility': 10,
'ozone': 306.76},
现在这是一个有点复杂的json文件,我尝试在json文件的每小时部分使用json_normalize:
json_normalize(data['hourly'])
但是我得到这样的响应:
data icon summary
0 [{'time': 1553050800, 'summary': 'Clear', 'ico... clear-day Clear
throughout the day.
任何想法我如何访问每小时数据部分的温度和压力? 因此,我需要获取每小时可用的时间,温度,压力和湿度。 谢谢
答案 0 :(得分:2)
您应该能够通过以下方式访问每小时数据中的第一个元素
data['hourly']['data'][0]['humidity']
因此,如果您遍历data ['hourly'] ['data'],则应该能够获取所需的数据。
例如
for data_point in data['hourly']['data']:
print data_point['humidity']
答案 1 :(得分:1)
您可以转换数据,使其仅包含您感兴趣的值:
代码:
import requests
import json
url = 'https://api.darksky.net/forecast/api_key/33.972386,-84.231986'
response = requests.get(url)
data = response.json()
fields = ['time', 'temperature', 'pressure', 'humidity']
transformed = [
{
field: rec.get(field)
for field
in fields
}
for rec
in data['hourly']['data']
]
print(json.dumps(transformed, indent=4))
输出:
[
{
"time": 1553050800,
"temperature": 44.13,
"pressure": 1026.33,
"humidity": 0.48
},
{
"time": 1553054400,
"temperature": 42.82,
"pressure": 1026.44,
"humidity": 0.5
},
{
"time": 1553058000,
"temperature": 42.45,
"pressure": 1026.39,
"humidity": 0.51
},
{
"time": 1553061600,
"temperature": 41.97,
"pressure": 1025.98,
"humidity": 0.52
},
{
"time": 1553065200,
"temperature": 40.7,
"pressure": 1025.88,
"humidity": 0.55
}
]