“内容”和“文本”有什么区别

问题:“内容”和“文本”有什么区别

我正在使用很棒的Python Requests库。我注意到,精美的文档中有许多示例,说明了如何做某事而不解释其原因。例如,r.textr.content都作为如何获得服务器响应的示例显示。但是,这些属性在哪里解释呢?例如,我什么时候会选择一个?我看到thar 有时会r.text返回一个unicode对象,并且我想非文本响应会有所不同。但是,所有这些都记录在哪里?请注意,链接文档确实声明:

对于非文本请求,您还可以字节形式访问响应主体:

但随后继续显示文本响应的示例!我只能假设上面的引号是说non-text responses而不是non-text requests,因为非文本请求在HTTP中没有意义。

简而言之,相对于Python Requests网站上的(优秀)教程,该库的正确文档在哪里?

I am using the terrific Python Requests library. I notice that the fine documentation has many examples of how to do something without explaining the why. For instance, both r.text and r.content are shown as examples of how to get the server response. But where is it explained what these properties do? For instance, when would I choose one over the other? I see thar r.text returns a unicode object sometimes, and I suppose that there would be a difference for a non-text response. But where is all this documented? Note that the linked document does state:

You can also access the response body as bytes, for non-text requests:

But then it goes on to show an example of a text response! I can only suppose that the quote above means to say non-text responses instead of non-text requests, as a non-text request does not make sense in HTTP.

In short, where is the proper documentation of the library, as opposed to the (excellent) tutorial on the Python Requests site?


回答 0

开发接口进行了详细介绍:

r.text是Unicode中响应的内容,并且r.content是字节中响应的内容。

The requests.Response class documentation has more details:

r.text is the content of the response in Unicode, and r.content is the content of the response in bytes.


回答 1

从文档中可以明显看出,r.content

You can also access the response body as bytes, for non-text requests:

 >>> r.content

如果您进一步阅读该页面,它会处理例如图像文件

It seems clear from the documentation is that r.content

You can also access the response body as bytes, for non-text requests:

 >>> r.content

If you read further down the page it addresses for example an image file