Python urllib2: get JSON response from url

I am trying to get the 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 is of type str, and I expect JSON. Is there a way to capture the response as JSON or a python dictionary instead of str.

+87
json python urllib2
Dec 17
source share
10 answers

If the URL returns valid JSON data, use the json library to decode this:

 import urllib2 import json response = urllib2.urlopen('https://api.instagram.com/v1/tags/pizza/media/XXXXXX') data = json.load(response) print data 
+179
Dec 17
source share
 import json import urllib url = 'http://example.com/file.json' r = urllib.request.urlopen(url) data = json.loads(r.read().decode(r.info().get_param('charset') or 'utf-8')) print(data) 

urllib , for Python 3.4
HTTPMessage returned by r.info ()

+35
Mar 20 '14 at 10:47
source share

Be careful with checking, etc., but the direct solution is this:

 import json the_dict = json.load(response) 
+4
Dec 17
source share
 """ Return JSON to webpage Adding to wonderful answer by @Sanal For Django 3.4 Adding a working url that returns a json (Source: http://www.jsontest.com/#echo) """ import json import urllib url = 'http://echo.jsontest.com/insert-key-here/insert-value-here/key/value' respons = urllib.request.urlopen(url) data = json.loads(respons.read().decode(respons.info().get_param('charset') or 'utf-8')) return HttpResponse(json.dumps(data), content_type="application/json") 
+3
Mar 15 '17 at 15:01
source share
 resource_url = 'http://localhost:8080/service/' response = json.loads(urllib2.urlopen(resource_url).read()) 
+2
Apr 17 '14 at 8:13
source share

Python 3 standard library on one line:

 load(urlopen(url)) # imports (place these above the code before running it) from json import load from urllib.request import urlopen url = 'https://jsonplaceholder.typicode.com/todos/1' 
+1
Apr 19 '19 at 3:10
source share

Although I guess I already answered, I would like to add my little bit to this

 import json import urllib2 class Website(object): def __init__(self,name): self.name = name def dump(self): self.data= urllib2.urlopen(self.name) return self.data def convJSON(self): data= json.load(self.dump()) print data domain = Website("https://example.com") domain.convJSON() 

Note: the object passed to json.load () must support .read () , so urllib2.urlopen (self.name) .read () will not work. Doamin wire must be provided with a protocol in this case http

0
Jan 12 '17 at 6:42 on
source share

You can also get JSON using requests , as shown below:

 import requests r = requests.get('http://yoursite.com/your-json-pfile.json') json_response = r.json() 
0
Aug 08 '19 at 6:00
source share

This is another simpler solution for your question.

 pd.read_json(data) 

where data is the output from the following code

 response = urlopen("https://data.nasa.gov/resource/y77d-th95.json") json_data = response.read().decode('utf-8', 'replace') 
0
Aug 30 '19 at 14:49
source share

None of the examples here worked for me. They were either for Python 2 (uurllib2) or for Python 3, returning the error "ImportError: No module named request". I am sending a Google error message and apparently I need to install the module, which is clearly unacceptable for such a simple task.

This code worked for me:

 import json,urllib data = urllib.urlopen("https://api.github.com/users?since=0").read() d = json.loads(data) print (d) 
-one
Feb 23 '16 at 19:56
source share



All Articles