gpt4 book ai didi

python - 如何显示json的特定部分?

转载 作者:太空宇宙 更新时间:2023-11-04 08:46:55 24 4
gpt4 key购买 nike

有人可以帮助我使用这个 python api 调用程序吗?

import json
from pprint import pprint
import requests
weather = requests.get('http://api.openweathermap.org/data/2.5/weather?
q=London&APPID=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')
pprint(weather.json())

wjson = weather.read()
wjdata = json.load(weather)
print (wjdata['temp_max'])

因此,使用这段代码,我试图从天气 api 获取信息,它会正确打印它,但是当我只想选择某些值时,我会收到此错误。

Traceback (most recent call last):
File "gawwad.py", line 7, in <module>
wjson = weather.read()
AttributeError: 'Response' object has no attribute 'read'

最佳答案

.json()是内置在requests中的JSON解码器,不需要单独解析JSON:

import requests

weather = requests.get('http://api.openweathermap.org/data/2.5/weather?q=London&APPID=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')
wjdata = weather.json()
print (wjdata['temp_max'])

关于python - 如何显示json的特定部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39602939/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com