user4754843
user4754843

Reputation:

JSONDecode Error

I make a requests on IMDb to fetch movie information into JSON. Here's my code:

import json
import requests
url = "http://www.imdb.com/title/tt3385516/"
re = requests.get(url).json()

It get me an error which I don't know what to do with it:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.5/dist-packages/requests/models.py", line 799, in json
    return json.loads(self.text, **kwargs)
  File "/usr/lib/python3.5/json/__init__.py", line 319, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.5/json/decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.5/json/decoder.py", line 357, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 8 column 1 (char 7)

I tried using

re = requests.get(url)
data = json.loads(re.text)

But it get me another error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.5/json/__init__.py", line 319, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.5/json/decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.5/json/decoder.py", line 357, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 8 column 1 (char 7)

Upvotes: 1

Views: 2373

Answers (1)

niemmi
niemmi

Reputation: 17273

Response is HTML, not JSON so it can't be JSON decoded:

>>> r = requests.get('http://www.imdb.com/title/tt3385516/')
>>> r.headers['content-type']
'text/html;charset=UTF-8'

Upvotes: 2

Related Questions