简体   繁体   English

从stackoverflow rest api读取数据

[英]reading data from stackoverflow rest api

Playing around with the raspberry pi and python. 玩树莓派和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 当尝试从stackoverflow api解码响应数据时,我继续收到错误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? 但是响应数据是以utf-8格式编码的吗? 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 :-) 尝试解码未压缩的数据:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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