在我的一个脚本中得到以下代码:

#
# url is defined above.
#
jsonurl = urlopen(url)

#
# While trying to debug, I put this in:
#
print jsonurl

#
# Was hoping text would contain the actual json crap from the URL, but seems not...
#
text = json.loads(jsonurl)
print text

我要做的是获得{{.....等.....}}东西,我看到的URL,当我在Firefox加载到我的脚本,所以我可以解析出一个值。我已经谷歌了很多,但我还没有找到一个很好的答案,如何实际得到{{…}}将以.json结尾的URL转换为Python脚本中的对象。


当前回答

对于python>=3.6,您可以使用:

import dload

j = dload.json(url)

安装dload:

pip3 install dload

其他回答

没有必要使用额外的库来解析json…

Json.loads()返回一个字典。

在你的例子中,只需输入text["someValueKey"]

不确定为什么前面的答案都使用json.loads。你只需要:

import json
from urllib.request import urlopen

f = urlopen("https://www.openml.org/d/40996/json")
j = json.load(f)

这是因为urlopen返回一个类似文件的对象,它与json.load一起工作。

对于python>=3.6,您可以使用:

import dload

j = dload.json(url)

安装dload:

pip3 install dload

调用urlopen()所做的(根据文档)就是返回一个类文件对象。一旦你有了这个,你需要调用它的read()方法来在网络上实际拉出JSON数据。

喜欢的东西:

jsonurl = urlopen(url)

text = json.loads(jsonurl.read())
print text

我发现这是在使用Python 3时从网页获取JSON的最简单和最有效的方法:

import json,urllib.request
data = urllib.request.urlopen("https://api.github.com/users?since=100").read()
output = json.loads(data)
print (output)