请求返回字节,但我无法对其进行解码 [英] Request returns bytes and I'm failing to decode them

查看:140
本文介绍了请求返回字节,但我无法对其进行解码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

本质上,我向网站提出了一个请求,并返回了一个字节响应:b'[{"geonameId:"703448"}..........'.我很困惑,因为尽管它是字节类型的,但它还是很容易理解的,并且看起来像json列表.我确实知道响应是从运行r.encoding的latin1中编码的,返回了ISO-859-1,并且我尝试对其进行解码,但是它只是返回一个空字符串.这是我到目前为止的内容:

Essentially I made a request to a website and got a byte response back: b'[{"geonameId:"703448"}..........'. I'm confused because although it is of type byte, it is very human readable and appears like a list of json. I do know that the response is encoded in latin1 from running r.encoding which returned ISO-859-1 and I have tried to decode it, but it just returns an empty string. Here's what I have so far:

r = response.content
string = r.decode("ISO-8859-1")
print (string)

,它在这里打印空白行. 但是,当我运行

and this is where it prints a blank line. However when I run

len(string)

我得到:返回31023 如何在不返回空字符串的情况下解码这些字节?

I get: back 31023 How can I decode these bytes without getting back an empty string?

推荐答案

您尝试使用json模块进行解析吗?

Did you try to parse it with the json module?

import json
parsed = json.loads(response.content)

这篇关于请求返回字节,但我无法对其进行解码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆