Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

I'm using Python 3.4 and have confirmed that json and urllib3 have been 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 get:

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 out so many new things (API, urllib3, json), I'm not exactly sure what's going on. I've tried doing a few 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 spot it?

like image 872
alanafrankly Avatar asked Aug 07 '14 18:08

alanafrankly


People also ask

How do I use urllib3 in Python?

Python urllib3 send JSON The example sends JSON data. We encode the JSON data into binary format. We specify the Content-Type header in the request. We decode the returned data back to text and print it to the console.

Is urllib3 a standard Python library?

The Python 3 standard library has a new urllib which is a merged/refactored/rewritten version of the older modules. urllib3 is a third-party package (i.e., not in CPython's standard library).

How do I import from urllib3 to Python?

Type "cmd" in the search bar and hit Enter to open the command line. What is this? Type “ pip install urllib3 ” (without quotes) in the command line and hit Enter again. This installs urllib3 for your default Python installation.

Is urllib3 built in Python?

urllib and urllib2 are built-in for Python 2, and were merged and reorganized as just urllib in Python 3. urllib3 is a third-party module. This is correct.


1 Answers

You'll need to decode the network data to a a string:

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

Ideally, you'd detect what codec was used from the Content-Type header; you'd parse that and look for a charset= parameter. The default encoding for JSON data is UTF-8.

If you are going to use a 3rd party library, use requests instead. 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())
like image 118
Martijn Pieters Avatar answered Oct 15 '22 10:10

Martijn Pieters