Python urllib2:从url接收JSON响应 [英] Python urllib2: Receive JSON response from url

查看:109
本文介绍了Python urllib2:从url接收JSON响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Python获取URL,响应为JSON.但是,当我跑步

I am trying to GET a URL using Python and the response is JSON. However, when I run

import urllib2
response = urllib2.urlopen('https://api.instagram.com/v1/tags/pizza/media/XXXXXX')
html=response.read()
print html

html的类型为str,我期望使用JSON.有什么方法可以将响应捕获为JSON或python字典而不是str.

The html is of type str and I am expecting a JSON. Is there any way I can capture the response as JSON or a python dictionary instead of a str.

推荐答案

如果URL返回的是有效的JSON编码数据,请使用

If the URL is returning valid JSON-encoded data, use the json library to decode that:

import urllib2
import json

response = urllib2.urlopen('https://api.instagram.com/v1/tags/pizza/media/XXXXXX')
data = json.load(response)   
print data

这篇关于Python urllib2:从url接收JSON响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆