擅长:python、mysql、java
<p>从Python2.X和Python3.X的网页中获取JSON格式的字典:</p>
<pre><code>#!/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))
</code></pre>
<p>另请参见:<a href="https://stackoverflow.com/a/37795053/562769">Read and write example for JSON</a></p>