twitter 趋势 api UnicodeDecodeError:'utf8' 编解码器无法解码位置 1 的字节 0x8b:意外的代码字节 [英] twitter trends api UnicodeDecodeError: 'utf8' codec can't decode byte 0x8b in position 1: unexpected code byte
问题描述
我正在尝试遵循挖掘社交网络"一书的示例代码,1-3.
I am trying to follow the sample code of the book "Mining the social web", 1-3.
我知道它是旧的,所以我遵循网页中的新示例在此处输入链接描述
I know its old so I follow the new sample from the web page enter link description here
但是,有时,我在执行代码时会遇到错误信息:
BUT, SOMETIMES, I will suffer a Error info when I implement the code:
[ trend.decode('utf-8') for trend in world_trends()[0]['trends'] ]
错误信息是这样的:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "build/bdist.macosx-10.6-universal/egg/twitter/api.py", line 167, in __call__
File "build/bdist.macosx-10.6-universal/egg/twitter/api.py", line 173, in _handle_response
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/encodings/utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0x8b in position 1: unexpected code byte
它并不总是发生,但我认为没有程序员喜欢这种随机"的情况.
It doesnt always happen, but I think no programmer likes such a "random" case.
那么有人可以帮我解决这个问题吗?这是什么问题,我该如何解决?
So could anyone help me on this issue? Whats the problem and how I can solve this?
非常感谢~
推荐答案
byte 0x8b in position 1
通常表示数据流被 gzip 压缩.对于类似问题,请参阅此处和此处.
byte 0x8b in position 1
usually signals that the data stream is gzipped. For similar problems, see here and here.
解压数据流:
buf = StringIO.StringIO(<response object>.content)
gzip_f = gzip.GzipFile(fileobj=buf)
content = gzip_f.read()
这篇关于twitter 趋势 api UnicodeDecodeError:'utf8' 编解码器无法解码位置 1 的字节 0x8b:意外的代码字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!