0
votes

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?

2

2 Answers

2
votes

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())
0
votes

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/