如何从网页获取JSON到Python脚本

时间:2012-10-18 23:20:37

标签: python json

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

#
# 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中加载的{{.....etc.....}}内容,当我将它加载到我的脚本中时,我可以解析它的值。我用谷歌搜索了一下,但我没有找到一个很好的答案,如何实际从{{...}}结尾的URL获取.json内容到Python脚本中的对象。

11 个答案:

答案 0 :(得分:253)

从网址获取数据,然后拨打json.loads例如

Python2示例

import urllib, json
url = "http://maps.googleapis.com/maps/api/geocode/json?address=google"
response = urllib.urlopen(url)
data = json.loads(response.read())
print data

Python3示例

import urllib.request, json 
with urllib.request.urlopen("http://maps.googleapis.com/maps/api/geocode/json?address=google") as url:
    data = json.loads(url.read().decode())
    print(data)

输出结果会是这样的:

{
"results" : [
    {
    "address_components" : [
        {
            "long_name" : "Charleston and Huff",
            "short_name" : "Charleston and Huff",
            "types" : [ "establishment", "point_of_interest" ]
        },
        {
            "long_name" : "Mountain View",
            "short_name" : "Mountain View",
            "types" : [ "locality", "political" ]
        },
        {
...

答案 1 :(得分:89)

我猜你真的想从网址获取数据:

jsonurl = urlopen(url)
text = json.loads(jsonurl.read()) # <-- read from it

或者,请查看JSON decoder库中的requests

import requests
r = requests.get('someurl')
print r.json() # if response type was set to JSON, then you'll automatically have a JSON response here...

答案 2 :(得分:24)

这将从使用Python 2.X和Python 3.X的网页获取JSON格式的字典:

#!/usr/bin/env python

try:
    # For Python 3.0 and later
    from urllib.request import urlopen
except ImportError:
    # Fall back to Python 2's urllib2
    from urllib2 import urlopen

import json


def get_jsonparsed_data(url):
    """
    Receive the content of ``url``, parse it as JSON and return the object.

    Parameters
    ----------
    url : str

    Returns
    -------
    dict
    """
    response = urlopen(url)
    data = response.read().decode("utf-8")
    return json.loads(data)


url = ("http://maps.googleapis.com/maps/api/geocode/json?"
       "address=googleplex&sensor=false")
print(get_jsonparsed_data(url))

另请参阅:Read and write example for JSON

答案 3 :(得分:18)

我发现这是使用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)

答案 4 :(得分:4)

最新答案,但是对于python>=3.6,您可以使用:

import dload
j = dload.json(url)

通过以下方式安装dload

pip3 install dload

答案 5 :(得分:4)

您需要 Here is JSON: ```json { "money": 32, "currencyUnit": "EUR", "clientId": "Berik7182", "name": "Berik", "email": "berik@gmail.com", "password": "Berik123", "active": true, "roles": "ROLE_USER", "location": "Pecs", "pwcons": 30.0 } 并使用json()方法:

import requests

当然,此方法也适用:

source = requests.get("url").json()
print(source)

import json,urllib.request data = urllib.request.urlopen("url").read() output = json.loads(data) print (output) 将使用此table将其解码为Python对象,例如JSON对象将变为Python json.loads

答案 6 :(得分:3)

urlopen()的所有调用(根据docs)都返回一个类似文件的对象。完成后,您需要调用其read()方法来实际通过网络提取JSON数据。

类似的东西:

jsonurl = urlopen(url)

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

答案 7 :(得分:2)

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

json.loads()会返回dictionary

所以在你的情况下,只需做text["someValueKey"]

答案 8 :(得分:2)

在Python 2中,json.load()将起作用而不是json.loads()

import json
import urllib

url = 'https://api.github.com/users?since=100'
output = json.load(urllib.urlopen(url))
print(output)

不幸的是,这在Python 3中不起作用.json.load只是json.loads的一个包装器,它为一个类似文件的对象调用read()。 json.loads需要一个字符串对象,urllib.urlopen(url).read()的输出是一个bytes对象。因此,必须获取文件编码才能使其在Python 3中运行。

在这个例子中,我们查询编码的标题,如果我们没有得到,则回退到utf-8。 header 2和3之间的headers对象是不同的,因此它必须以不同的方式完成。使用requests可以避免所有这些,但有时您需要坚持使用标准库。

import json
from six.moves.urllib.request import urlopen

DEFAULT_ENCODING = 'utf-8'
url = 'https://api.github.com/users?since=100'
urlResponse = urlopen(url)

if hasattr(urlResponse.headers, 'get_content_charset'):
    encoding = urlResponse.headers.get_content_charset(DEFAULT_ENCODING)
else:
    encoding = urlResponse.headers.getparam('charset') or DEFAULT_ENCODING

output = json.loads(urlResponse.read().decode(encoding))
print(output)

答案 9 :(得分:0)

不确定为什么所有早期答案都使用 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 一起使用。

答案 10 :(得分:-1)

您可以使用json.dumps

import json

# Hier comes you received data

data = json.dumps(response)

print(data)

用于加载json并将其写入文件,以下代码非常有用:

data = json.loads(json.dumps(Response, sort_keys=False, indent=4))
with open('data.json', 'w') as outfile:
json.dump(data, outfile, sort_keys=False, indent=4)