Problem with python + json + urllib error json.decoder.Jsondecodeerror

Asked

Viewed 233 times

0

I cannot use json and urllib I get an error, I think the error is because of the system, I use windows! I have already researched a lot and I see no solution to this error. The code is very simple :

    import json
from urllib.request import urlopen, Request


with urlopen('http://python.org/') as response:
   html = response.read().decode('utf-8')

print(html)

data = json.loads(html)

print(json.dumps(data, indent=2))

error occurs:

    File "D:\Marcus\Projetos\Programação\Python\Study\Json\HOWTO Fetch Internet Resources\urllib_json.py", line 10, in <module>
    data = json.loads(html)
  File "C:\Program Files\Python36\lib\json\__init__.py", line 354, in loads
    return _default_decoder.decode(s)
  File "C:\Program Files\Python36\lib\json\decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Program Files\Python36\lib\json\decoder.py", line 357, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Use: Python 3.6.5, Windows 10

If anyone can help me, I’d appreciate it!

  • I think it’s because you’re not passing a json

  • Hello, Anderson. I’m a beginner in language, I started to hold a few months. and I didn’t understand what you wrote as so "it’s not a json passing" ?

  • Anderson. thank you very beginner doubt, I think I get it now. Thanks really guy!

  • You are taking an html and passing it to a function that receives a json, so the function does not work because it was not passed a json but an html code

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.