解析请求响应时应该使用.text还是.content? [英] Should I use .text or .content when parsing a Requests response?

查看:108
本文介绍了解析请求响应时应该使用.text还是.content?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有时会使用res.contentres.text来解析来自请求的响应.在我使用过的用例中,使用哪个选项似乎无关紧要.

I occasionally use res.content or res.text to parse a response from Requests. In the use cases I have had, it didn't seem to matter which option I used.

使用.content.text解析HTML的主要区别是什么?例如:

What is the main difference in parsing HTML with .content or .text? For example:

import requests 
from lxml import html
res = requests.get(...)
node = html.fromstring(res.content)

在上述情况下,我应该使用res.content还是res.text?什么时候使用每种的最佳经验法则是什么?

In the above situation, should I be using res.content or res.text? What is a good rule of thumb for when to use each?

推荐答案

来自文档:

发出请求时,请求会针对 基于HTTP标头的响应编码.文字编码 访问r.text时,将使用请求"猜测的值.你可以找出来 请求使用的是哪种编码,并使用r.encoding进行更改 属性:

When you make a request, Requests makes educated guesses about the encoding of the response based on the HTTP headers. The text encoding guessed by Requests is used when you access r.text. You can find out what encoding Requests is using, and change it, using the r.encoding property:

>>> r.encoding
'utf-8'
>>> r.encoding = 'ISO-8859-1'

如果您更改编码,则请求将使用的新值 每次调用r.textr.encoding.您可能想要在任何情况下执行此操作 在这种情况下,您可以应用特殊的逻辑来计算出 内容将被编码.例如,HTTP和XML具有 可以在其主体中指定其编码的能力.在类似的情况下 为此,您应该使用r.content查找编码,然后进行设置 r.encoding.这样您就可以使用r.text正确的编码了.

If you change the encoding, Requests will use the new value of r.encoding whenever you call r.text. You might want to do this in any situation where you can apply special logic to work out what the encoding of the content will be. For example, HTTP and XML have the ability to specify their encoding in their body. In situations like this, you should use r.content to find the encoding, and then set r.encoding. This will let you use r.text with the correct encoding.

因此,当服务器返回二进制数据或伪造的编码标头时,将使用r.content来尝试在元标记中找到正确的编码.

So r.content is used when the server returns binary data, or bogus encoding headers, to try to find the correct encoding inside a meta tag.

这篇关于解析请求响应时应该使用.text还是.content?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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