Skip to main content
 首页 » 编程设计

python之如何显示json的特定部分

2025年05月04日26freeliver54

有人可以帮助我使用这个 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'])