如何在python中解码网页响应

2024-09-29 01:26:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我是这样解码gzip电子编码的:

gzipper = gzip.GzipFile(fileobj=wepage_response)
decoded_str = str(gzipper)

现在解码的字符串如下所示:

^{pr2}$

我该怎么解释?在

谢谢你


Tags: 字符串编码response电子解码decodedfileobjgzip
1条回答
网友
1楼 · 发布于 2024-09-29 01:26:57

您需要read()来自gzipper实例的内容:

import gzip
from cStringIO import StringIO

webpage_response = '\x1f\x8b\x08\x08\xe6|\xdfS\x00\x03test.txt\x00\xcb\xc8T(\xc9H-J\xe5\x02\x00\xb6\x0b\x88\x9b\t\x00\x00\x00'
gzipper = gzip.GzipFile(fileobj=StringIO(webpage_response))
decompressed_str = gzipper.read()

>>> decompressed_str
'hi there\n'

相关问题 更多 >