1
votes

I am trying to port a code that is working fine on Python2 to Python3. I tried to fix some bugs but now it is stuking with the json() on a class of . Code shows below

resp = requests.get("https://status.github.com/api/status.json")
print(resp.ok)          
print(resp.status_code) 
print(resp.headers['content-type'])
print(resp)
x = resp.json()

The debug results are below

> True 200 text/html; charset=utf-8 <Response [200]>
> --------------------------------------------------------------------------- JSONDecodeError                           Traceback (most recent call
> last) <ipython-input-40-98ddeb8de13d> in <module>()
>       4 print(resp.headers['content-type'])
>       5 print(resp)
> ----> 6 x = resp.json()
> 
> D:\ProgramData\Anaconda\lib\site-packages\requests\models.py in
> json(self, **kwargs)
>     894                     # used.
>     895                     pass
> --> 896         return complexjson.loads(self.text, **kwargs)
>     897 
>     898     @property
> 
> D:\ProgramData\Anaconda\lib\json\__init__.py in loads(s, encoding,
> cls, object_hook, parse_float, parse_int, parse_constant,
> object_pairs_hook, **kw)
>     346             parse_int is None and parse_float is None and
>     347             parse_constant is None and object_pairs_hook is None and not kw):
> --> 348         return _default_decoder.decode(s)
>     349     if cls is None:
>     350         cls = JSONDecoder
> 
> D:\ProgramData\Anaconda\lib\json\decoder.py in decode(self, s, _w)
>     335 
>     336         """
> --> 337         obj, end = self.raw_decode(s, idx=_w(s, 0).end())
>     338         end = _w(s, end).end()
>     339         if end != len(s):
> 
> D:\ProgramData\Anaconda\lib\json\decoder.py in raw_decode(self, s,
> idx)
>     353             obj, end = self.scan_once(s, idx)
>     354         except StopIteration as err:
> --> 355             raise JSONDecodeError("Expecting value", s, err.value) from None
>     356         return obj, end
> 

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

1

1 Answers

0
votes
resp = requests.get("https://status.github.com/api/status.json")         
print(resp.headers['content-type'])

text/html; charset=utf-8

Use resp.headers['content-type'] to find that the link web is of text/html.

Hence the resp.json() would not be applicable for this format. Maybe you want to try Beautifulsoup.