Error when trying to use urllib3 & json to get Rotten Tomatoes data (Python)

As an introduction to the API, I'm trying to figure out how to access data in python using the Rotten Tomatoes API. This is also my first experience with json.

I am using Python 3.4 and confirmed that json and urllib3 are installed.

Here is my code:

import urllib3
import json

url = 'http://api.rottentomatoes.com/api/public/v1.0/lists/movies/box_office.json?limit=16&country=us&apikey=API-KEY';

http = urllib3.PoolManager()
request = http.request('GET', url)
print (json.load(request));
request.release_conn()

      

Here is the error I am getting:

Traceback (most recent call last):
  File "C:\Users\admnasst1\Documents\Personal\Python\RotTomTest.py", line 16, in <module>
    print (str(json.load(request)));
  File "C:\Python34\lib\json\__init__.py", line 268, in load
    parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
  File "C:\Python34\lib\json\__init__.py", line 312, in loads
    s.__class__.__name__))
TypeError: the JSON object must be str, not 'bytes'

      

Since I'm trying so many new things (API, urllib3, json) I'm not really sure what's going on. I have tried several other versions of the above code and I keep getting the same error, so I think I must be missing something basic ... Can any of you figure it out?

+3


source to share


1 answer


You need to decode the network data into a string:

json.loads(request.data.decode('utf8'))

      

Ideally, you should determine which codec was used in the header Content-Type

; you will analyze this and find the parameter charset=

. The default encoding for JSON data is UTF-8.



If you are going to use a third party library use requests

. It wraps urllib3

in a friendly API and can handle JSON for you:

import requests

url = 'http://api.rottentomatoes.com/api/public/v1.0/lists/movies/box_office.json'
params = {'limit': 16, 'country': 'us', 'apikey': 'API-KEY'}

response = requests.get(url, params=params)
print(response.json())

      

+6


source







All Articles