如何用python解析json api响应? 我现在有这个:
import urllib.request
import json
url = 'https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty'
def response(url):
with urllib.request.urlopen(url) as response:
return response.read()
res = response(url)
print(json.loads(res))
我得到这个错误: TypeError:JSON对象必须是str,而不是'bytes'
处理json api的pythonic方法是什么?
版本1:(在运行脚本之前执行
pip install requests
)版本2:(在运行脚本之前执行
pip install wget
)您可以使用标准库python3:
输出如下:
我通常将
requests
包与json
包一起使用。以下代码应适合您的需要:输出
相关问题 更多 >
编程相关推荐