Python Stackexchange API编码
我正在为Stackexchange API编写以下装饰程序:Python Stackexchange API编码,python,encoding,stackexchange-api,Python,Encoding,Stackexchange Api,我正在为Stackexchange API编写以下装饰程序: class StackOverflowHandler(tornado.web.RequestHandler): def get(self, look_up_pattern): url = "https://api.stackexchange.com/2.2/search?order=desc&sort=votes&intitle=%s&site=
class StackOverflowHandler(tornado.web.RequestHandler):
def get(self, look_up_pattern):
url = "https://api.stackexchange.com/2.2/search?order=desc&sort=votes&intitle=%s&site=stackoverflow"
with urllib.request.urlopen(url % look_up_pattern) as so_response:
response = so_response.read()
print(response)
self.write(response)
application = tornado.web.Application([
(r"/search/(.*)", StackOverflowHandler),
])
作为响应
我得到字节流:
b'\x1f\x8b\x08\x00\x00\x00\x00\x00\x04\x00\xb5\\\x0b\x93\xa3F\x92\xfe+u\xe...
问题是谁对回应进行编码?什么是正确的Unicode来解码?我检查了utf-8、utf-16、zlib.decompress等。。这没用 丹尼尔·罗斯曼的答案的相关部分如下:
if response.info().get('Content-Encoding') == 'gzip':
buf = StringIO( response.read())
f = gzip.GzipFile(fileobj=buf)
data = f.read()
换句话说,编码应该作为
response.info().get('Content-encoding')
提供,只需使用请求
或查看手动解压缩即可。