解析请求响应时应该使用.text还是.content? [英] Should I use .text or .content when parsing a Requests response?
问题描述
我有时会使用res.content
或res.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 ther.encoding
property:
>>> r.encoding
'utf-8'
>>> r.encoding = 'ISO-8859-1'
如果您更改编码,则请求将使用的新值 每次调用
r.text
时r.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 callr.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 user.content
to find the encoding, and then setr.encoding
. This will let you user.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屋!