Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python urllib2: Receive JSON response from url

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 

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.

like image 422
Deepak B Avatar asked Dec 17 '12 20:12

Deepak B


People also ask

How do I get JSON data from a website?

The first step in this process is to choose a web scraper for your project. We obviously recommend ParseHub. Not only is it free to use, but it also works with all kinds of websites. With ParseHub, web scraping is as simple as clicking on the data you want and downloading it as an excel sheet or JSON file.


1 Answers

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 
like image 181
Martijn Pieters Avatar answered Sep 27 '22 20:09

Martijn Pieters