简体   繁体   中英

reading data from stackoverflow rest api

Playing around with the raspberry pi and python. so bear with me :)

When trying to decode the response data from the stackoverflow api I keep on receiving the error utf-8 codec can't decode byte 0x8b in position 1: invalid start byte

Here is the entire code that I have hacked up at the moment

import http.client
import json
import zlib

c = http.client.HTTPConnection('api.stackoverflow.com')
c.request('GET', '/1.1/questions?answers=true&page=1&pagesize=5&tagged=sql')
r = c.getresponse()

compressedData = r.read()
uncompressedData= zlib.decompress(compressedData, 15+32)

data = str(compressedData, 'utf-8')
print(data)

But the response data is encoded in utf-8 format? Not quite sure why this is happening...

Your code looks fine but...

data = str(compressedData, 'utf-8')

you're trying to decode the compressed data. Try decoding the uncompressed data :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM