1

I got Jsonerror while fetching data from the youtube. JSONDecodeError: Expecting value: line 2 column 5 (char 5). My expected out is json. Iam able to fetch from different url url = 'https://api.github.com/repos/pandas-dev/pandas/issues'.

My Code is below

import requests
youtuburl ='https://www.youtube.com/feed/trending'
response_youtuburl = requests.get(youtuburl)
response_youtuburl
#<Response [200]>

print(type(response_youtuburl))
#<class 'requests.models.Response'>

data_youtuburl = response_youtuburl.json()
print(type(data_youtuburl))

Error is below

JSONDecodeError                           Traceback (most recent call last)
<ipython-input-32-a8d1beb0b96c> in <module>()
----> 1 data_youtuburl = response_youtuburl.json()
      2 print(type(data_youtuburl))
~/anaconda3/lib/python3.6/site-packages/requests/models.py in json(self, **kwargs)
    890                     # used.
    891                     pass
--> 892         return complexjson.loads(self.text, **kwargs)
    893 
    894     @property
~/anaconda3/lib/python3.6/json/__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    352             parse_int is None and parse_float is None and
    353             parse_constant is None and object_pairs_hook is None and not kw):
--> 354         return _default_decoder.decode(s)
    355     if cls is None:
    356         cls = JSONDecoder
~/anaconda3/lib/python3.6/json/decoder.py in decode(self, s, _w)
    337 
    338         """
--> 339         obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    340         end = _w(s, end).end()
    341         if end != len(s):
~/anaconda3/lib/python3.6/json/decoder.py in raw_decode(self, s, idx)
    355             obj, end = self.scan_once(s, idx)
    356         except StopIteration as err:
--> 357             raise JSONDecodeError("Expecting value", s, err.value) from None
    358         return obj, end
JSONDecodeError: Expecting value: line 2 column 5 (char 5)

1 Answer 1

1

The URL you are hitting (https://www.youtube.com/feed/trending) returns HTML, not JSON.

Sign up to request clarification or add additional context in comments.

1 Comment

My method was a web browser. In general, I imagine the programmer would know whether an URL returns JSON or not before trying to use it in a program. If for some reason that is not known, you could check response_youtuburl.headers['content-type']. In this case, it is 'text/html; charset=utf-8'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.