0

I am getting JSON from internet (Stackoverflow API) and trying to decode it:

import urllib.request

url = "https://api.stackexchange.com/2.2/questions?order=desc&sort=activity&site=stackoverflow"
fp = urllib.request.urlopen(url)
mybytes = fp.read()
mystr = mybytes.decode("utf8")
fp.close()

print(mystr)

I got the error:

Traceback (most recent call last): File "code.py", line 6, in mystr = mybytes.decode("utf8") UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte

Why and how can I fix it?

1

2 Answers 2

2

Use requests:

import requests

url = "https://api.stackexchange.com/2.2/questions?order=desc&sort=activity&site=stackoverflow"

res = requests.get(url)
if res.status_code == 200:
    print(res.json())
Sign up to request clarification or add additional context in comments.

Comments

0

Encoding and decoding utf-8 seems to be tricky in python. Shall I suggest including the .encode() as well.

import urllib.request

url = "https://api.stackexchange.com/2.2/questions?order=desc&sort=activity&site=stackoverflow"
fp = urllib.request.urlopen(url)
mybytes = fp.read()
mystr = mybytes.encode().decode()
fp.close()

print(mystr)

Also, yes, do use Requests. It's fantastic: http://docs.python-requests.org/en/master/

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.