有人可以帮我解决这个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'])
Run Code Online (Sandbox Code Playgroud)
所以使用这段代码,我试图从天气api获取信息,它正确打印它,但当我想选择某些值时,我得到这个错误.
Traceback (most recent call last):
File "gawwad.py", line 7, in <module>
wjson = weather.read()
AttributeError: 'Response' object has no attribute 'read'
Run Code Online (Sandbox Code Playgroud)
.json()是一个内置于requestsJSON解码器,无需单独解析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'])
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
156 次 |
| 最近记录: |